Computer Programming Contest Preparation

ToolBox - Source for: 102/10258/a.c



/home/toolbox/public_html/solutions/102/10258/a.c
    1 #include <stdio.h>
    2 #include <string.h>
    3 #include <sys/types.h>
    4 #include <sys/stat.h>
    5 #include <fcntl.h>
    6 #include <stdlib.h>
    7 #include <math.h>
    8 #include <stdint.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 
   16 
   17 /*
   18  *  Author: Isaac Traxler
   19  *    Date: 2017-01-26
   20  * Purpose: fun
   21  * Problem: 10258 - Contest Scoreboard
   22  */
   23 
   24 /*
   25  * This template reads data a specified number of times.
   26  */
   27 
   28 #define MAXBUFF 50
   29 #define MAX_TEAMS 102
   30 #define MAX_PROBLEMS 11
   31 
   32 #define NOT_COMPETING -1
   33 #define SOLVED TRUE
   34 #define UNSOLVED FALSE
   35 
   36 #define C 1    /* correct */
   37 #define I 2    /* incorrect, wrong */
   38 #define R 3    /* clarification request -- ignore */
   39 #define U 4    /* unjudged run */
   40 #define E 5    /* error -- ignore */
   41 
   42 typedef struct prolemStruct
   43 {
   44     /* STRUCT problemStruct */
   45     int correct;       /* has this problem been solved */
   46     int time;          /* time of correct submisson */
   47     int wrong;         /* number of incorrect submissions */
   48 } /* STRUCT problemStruct */
   49 problemType;
   50 
   51 typedef struct contestStruct
   52 {
   53     /* STRUCT contestStruct */
   54     int teamNumber;              /* once sorting is done, we will need team number */
   55     int solved;                  /* -1 before submission, 0 when a submission received, incremented on correct */
   56     int score;                   /* calculated final score after contest over */
   57     problemType p[MAX_PROBLEMS]; /* array of problem structs */
   58 } /* STRUCT contestStruct */
   59 contestType;
   60 
   61 int numberOfTimes;
   62 contestType contest[MAX_TEAMS];
   63 
   64 char buffer[MAXBUFF+2];
   65 int team;              /* global team number of current submission */
   66 int problem;           /* global problem number of current submmission */
   67 int time;              /* global sumission time of current submission */
   68 int judgement;         /* global judgement of current submission */
   69 
   70 void initContest()
   71 {
   72     /* FUNCTION voidContest */
   73     int i;
   74 
   75     for (i=1; MAX_TEAMS>i; i++)
   76         {
   77             /* for */
   78             contest[i].teamNumber = i;
   79             contest[i].solved = NOT_COMPETING;
   80         } /* for */
   81 } /* FUNCTION voidContest */
   82 
   83 void initTeam(int t)
   84 {
   85     /* FUNCTION initTeam */
   86     int i;
   87 
   88     contest[t].teamNumber = t;
   89     contest[t].score = 0;
   90     contest[t].solved = 0;
   91     for (i=1; MAX_PROBLEMS>i; i++)
   92         {
   93             /* for */
   94             contest[t].p[i].correct = UNSOLVED;
   95             contest[t].p[i].time = 0;
   96             contest[t].p[i].wrong = 0;
   97         } /* for */
   98 } /* FUNCTION initTeam */
   99 
  100 int compare(const void *p1, const void *p2)
  101 {
  102     /* FUNCTION compare */
  103     contestType * t1, * t2;
  104     int retval;
  105 
  106     t1 = (contestType *)p1;
  107     t2 = (contestType *)p2;
  108 
  109     if (t1->solved > t2->solved)
  110         {
  111             /* t1 better */
  112             retval = -1;
  113         } /* t1 better */
  114     else if (t1->solved < t2->solved)
  115         {
  116             /* t2 better */
  117             retval = 1;
  118         } /* t2 better */
  119     else if (t1->score > t2->score)
  120         {
  121             /* t2 better by points */
  122             retval = 1;
  123         } /* t2 better by points */
  124     else if (t1->score < t2->score)
  125         {
  126             /* t1 better by points */
  127             retval = -1;
  128         } /* t1 better by points */
  129     else if (t1->teamNumber > t2->teamNumber)
  130         {
  131             /* t2 lower team number */
  132             retval = 1;
  133         } /* t2 lower team number */
  134     else
  135         {
  136             /* t1 lower team number */
  137             retval = -1;
  138         } /* t1 lower team number */
  139 
  140     return retval;
  141 } /* FUNCTION compare */
  142 
  143 void init()
  144 {
  145     /* FUNCTION init */
  146     scanf(" %d ", &numberOfTimes);
  147     /* skip first blank line */
  148     /*
  149      * fgets(buffer, MAXBUFF, stdin);
  150      */
  151 } /* FUNCTION init */
  152 
  153 void dump()
  154 {
  155     /* FUNCTION dump */
  156 } /* FUNCTION dump */
  157 
  158 int getInput()
  159 {
  160     /* FUNCTION getInput */
  161     int dataReadFlag;
  162     char tmp[10];
  163 
  164     dataReadFlag = NULL != fgets(buffer, MAXBUFF, stdin);
  165     DEBUG printf("line=[%s] length = %d\n", buffer, strlen(buffer));
  166     dataReadFlag = (dataReadFlag) && (4 < strlen(buffer)); /* if less than 4 chars, must be blank line */
  167     if (dataReadFlag)
  168         {
  169             /* something to parse */
  170             sscanf(buffer, " %d %d %d %s ", &team, &problem, &time, tmp);
  171             DEBUG printf(" %d %d %d %s [%c]\n", team, problem, time, tmp, tmp[0]);
  172             switch (tmp[0])
  173                 {
  174                 /* what judgement */
  175                 case 'C' :
  176                     judgement = C;
  177                     break;
  178                 case 'I' :
  179                     judgement = I;
  180                     break;
  181                 case 'R' :
  182                     judgement = R;
  183                     break;
  184                 case 'U' :
  185                     judgement = U;
  186                     break;
  187                 case 'E' :
  188                     judgement = E;
  189                     break;
  190                 default :
  191                     exit -99;
  192                 } /* what judgement */
  193         } /* something to parse */
  194     return dataReadFlag;
  195 } /* FUNCTION getInput */
  196 
  197 void doJudgement()
  198 {
  199     /* FUNCTION doJudgement */
  200     DEBUG printf(" %d %d %d %d\n", team, problem, time, judgement);
  201     switch (judgement)
  202         {
  203         /* switch on judgement */
  204         case C:
  205         {
  206             /* correct submission */
  207             contest[team].p[problem].correct = SOLVED;
  208             contest[team].p[problem].time = time;
  209             } /* correct submission */
  210         break;
  211         case I:
  212         {
  213             /* incorrect submission */
  214             contest[team].p[problem].wrong = 1 + contest[team].p[problem].wrong;
  215             DEBUG printf("wrong = %d\n", contest[team].p[problem].wrong);
  216             } /* incorrect submission */
  217         break;
  218         } /* switch on judgement */
  219 } /* FUNCTION doJudgement */
  220 
  221 void scoreTeams()
  222 {
  223     /* FUNCTION scoreTeams */
  224     int t;
  225     int p;
  226 
  227     for (t=1; MAX_TEAMS>t; t++)
  228         {
  229             /* process each team */
  230             if (NOT_COMPETING != contest[t].solved)
  231                 {
  232                     /* okay they competed */
  233                     for (p=1; MAX_PROBLEMS>p; p++)
  234                         {
  235                             /* for each problem */
  236                             if (SOLVED == contest[t].p[p].correct)
  237                                 {
  238                                     /* problem was solved */
  239                                     contest[t].solved = 1 + contest[t].solved;
  240                                     contest[t].score = contest[t].score + contest[t].p[p].time + 20 * contest[t].p[p].wrong;
  241                                 } /* problem was solved */
  242                         } /* for each problem */
  243                 } /* okay they competed */
  244         } /* process each team */
  245 } /* FUNCTION scoreTeams */
  246 
  247 void displayResults()
  248 {
  249     /* FUNCTION displayResults */
  250     int i;
  251 
  252     for (i=1; MAX_TEAMS>i; i++)
  253         {
  254             /* process each team */
  255             if (NOT_COMPETING != contest[i].solved)
  256                 {
  257                     /* okay they competed */
  258                     printf("%d %d %d\n", contest[i].teamNumber, contest[i].solved, contest[i].score);
  259                 } /* okay they competed */
  260         } /* process each team */
  261 
  262 } /* FUNCTION displayResults */
  263 
  264 void process()
  265 {
  266     /* FUNCTION process */
  267     int moreToDo;
  268 
  269     initContest();
  270     moreToDo = getInput();
  271     while (moreToDo)
  272         {
  273             /* while */
  274             if (NOT_COMPETING == contest[team].solved)
  275                 {
  276                     /* team just made first submission */
  277                     initTeam(team);
  278                     DEBUG printf(" contest[%d].solved = %d\n", team, contest[team].solved);
  279                 } /* team just made first submission */
  280             /* from here, we need only process judgements
  281              * if the problem is not yet solved
  282              */
  283             if (UNSOLVED == contest[team].p[problem].correct)
  284                 {
  285                     /* problem has not been solved before */
  286                     doJudgement();
  287                 } /* problem has not been solved before */
  288             moreToDo = getInput();
  289         } /* while */
  290 
  291     /* submissions have all been processed */
  292     scoreTeams();
  293     qsort(&contest[1], MAX_TEAMS-1, sizeof(contestType), compare);
  294     displayResults();
  295 } /* FUNCTION process */
  296 
  297 int main ()
  298 {
  299     /* main */
  300     int i;
  301 
  302     init();
  303     for (i=0; i<numberOfTimes; i++)
  304         {
  305             /* while */
  306             if (0 != i)
  307                 {
  308                     printf("\n");
  309                 }
  310             process();
  311         } /* while */
  312 
  313     return EXIT_SUCCESS;
  314 } /* main */
  315 
  316