Computer Programming Contest Preparation

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



/home/toolbox/public_html/solutions/102/10258/b.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 #define MAXBUFF 500
   16 #define MAXTEAM 100
   17 #define MAXPROB 9
   18 
   19 typedef struct
   20 {
   21     int num;
   22     int solved;
   23     int time;
   24 } teamStruct;
   25 
   26 /* fprintf(stderr, "functionName: message", varslist); */
   27 
   28 /*
   29  *  Author: Josh Abadie and Isaac Traxler
   30  *    Date: 2008 3 10
   31  * Purpose:
   32  * Problem: 10258 - Contest Scoreboard
   33  */
   34 
   35 /*
   36  * This template reads data a specified number of times.
   37  */
   38 
   39 int numberOfTimes;
   40 int contestArray[MAXTEAM+1][MAXPROB+1];
   41 int teamUsed[MAXTEAM+1];
   42 teamStruct results[MAXTEAM+1];
   43 int numCompeting = 0;
   44 int first  = 1;
   45 
   46 
   47 int compare(const void *p1, const void *p2)
   48 {
   49     /* FUNCTION compare */
   50     teamStruct * t1, * t2;
   51     int retval;
   52 
   53     t1 = (teamStruct *)p1;
   54     t2 = (teamStruct *)p2;
   55 
   56     if (t1->solved > t2->solved)
   57         {
   58             /* t1 better */
   59             retval = -1;
   60         } /* t1 better */
   61     else if (t1->solved < t2->solved)
   62         {
   63             /* t2 better */
   64             retval = 1;
   65         } /* t2 better */
   66     else if (t1->time > t2->time)
   67         {
   68             /* t2 better by points */
   69             retval = 1;
   70         } /* t2 better by points */
   71     else if (t1->time < t2->time)
   72         {
   73             /* t1 better by points */
   74             retval = -1;
   75         } /* t1 better by points */
   76     else if (t1->num > t2->num)
   77         {
   78             /* t2 lower team number */
   79             retval = 1;
   80         } /* t2 lower team number */
   81     else
   82         {
   83             /* t1 lower team number */
   84             retval = -1;
   85         } /* t1 lower team number */
   86 
   87     return retval;
   88 } /* FUNCTION compare */
   89 
   90 void init()
   91 {
   92     /* FUNCTION init */
   93     scanf("%d ", &numberOfTimes);
   94 } /* FUNCTION init */
   95 
   96 void dump()
   97 {
   98     /* FUNCTION dump */
   99     int i, j;
  100     printf ("START CASE\n");
  101     for ( i = 0; i < MAXTEAM; i ++)
  102         {
  103             if (0 != teamUsed[i])
  104                 {
  105                     printf("Team %d:", i);
  106                     for ( j = 0; j < MAXPROB; j ++)
  107                         {
  108                             printf(" (%d:%d)", j, contestArray[i][j]);
  109                         }
  110                     printf("\n");
  111                 }
  112         }
  113 } /* FUNCTION dump */
  114 
  115 void dump1()
  116 {
  117     /* FUNCTION dump1 */
  118     int i;
  119 
  120     for (i = 0; i < numCompeting; i ++)
  121         {
  122             printf("%d: %d %d %d\n", i, results[i].num, results[i].solved, results[i].time);
  123         }
  124 } /* FUNCTION dump1 */
  125 
  126 void dump2()
  127 {
  128     /* FUNCTION dump2 */
  129     int i;
  130 
  131     if(first == 1)
  132         {
  133             first = 0;
  134         }
  135     else
  136         {
  137             printf("\n");
  138         }
  139     for (i = 0; i < numCompeting; i ++)
  140         {
  141             printf("%d %d %d\n", results[i].num, results[i].solved, results[i].time);
  142         }
  143 } /* FUNCTION dump2 */
  144 
  145 void getInput()
  146 {
  147     /* FUNCTION getInput */
  148     char buffer[MAXBUFF];
  149     int team, prob, time;
  150     char type;
  151     int i, j;
  152 
  153     for (i = 0; i < MAXTEAM; i ++)
  154         {
  155             teamUsed[i] = 0;
  156             for (j = 0; j < MAXPROB; j ++)
  157                 {
  158                     contestArray[i][j] = 0;
  159                 }
  160         }
  161     numCompeting = 0;
  162 
  163     fgets(buffer, MAXBUFF-10, stdin);
  164     while (buffer[0] != '\n' && !feof(stdin) )
  165         {
  166             /*get input*/
  167             sscanf(buffer, "%d %d %d %c", &team, &prob, &time, &type);
  168             DEBUG printf("team:%d prob:%d time:%d type:%c\n", team, prob, time, type);
  169 
  170             teamUsed[team] = 1;
  171             switch (type)
  172                 {
  173                 /* switch */
  174                 case 'C' :
  175                     if (-1 < contestArray[team][prob])
  176                         {
  177                             contestArray[team][prob] = -(contestArray[team][prob] + time + 1);
  178                         }
  179                     break;
  180                 case 'I' :
  181                     if (-1 < contestArray[team][prob])
  182                         {
  183                             contestArray[team][prob] = contestArray[team][prob] + 20;
  184                         }
  185                 default :
  186                     break;
  187                 } /* switch */
  188 
  189             fgets(buffer, MAXBUFF-10, stdin);
  190         }/*get input*/
  191 } /* FUNCTION getInput */
  192 
  193 void process()
  194 {
  195     /* FUNCTION process */
  196     int i, j;
  197 
  198     for (i = 0; i < MAXTEAM; i ++)
  199         {
  200             /* for */
  201             if (0 < teamUsed[i])
  202                 {
  203                     /* team competed */
  204                     results[numCompeting].num = i;
  205                     results[numCompeting].solved = 0;
  206                     results[numCompeting].time = 0;
  207                     for (j = 0; j < MAXPROB; j ++)
  208                         {
  209                             /* calculate team results */
  210                             if (0 > contestArray[i][j])
  211                                 {
  212                                     /* negative means problem solved */
  213                                     results[numCompeting].solved++;
  214                                     results[numCompeting].time = results[numCompeting].time - contestArray[i][j] - 1;
  215                                 } /* negative means problem solved */
  216                         } /* calculate team results */
  217                     numCompeting++;
  218                 } /* team competed */
  219         } /* for */
  220     DEBUG printf("DUMP\n");
  221     DEBUG dump1();
  222     qsort(results, numCompeting, sizeof(teamStruct), compare);
  223     DEBUG printf("SORTED DUMP\n");
  224     DEBUG dump1();
  225     dump2();
  226 } /* FUNCTION process */
  227 
  228 int main ()
  229 {
  230     /* main */
  231     int i;
  232 
  233     init();
  234     for (i=0; i<numberOfTimes; i++)
  235         {
  236             /* while */
  237             getInput();
  238             process();
  239         } /* while */
  240 
  241     return EXIT_SUCCESS;
  242 } /* main */
  243 
  244