Computer Programming Contest Preparation

ToolBox - Source for: 113/11389/a.c



/home/toolbox/public_html/solutions/113/11389/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 <stdint.h>
    7 #include <math.h>
    8 #include <stdlib.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 
   15 /*
   16  *  Author: Isaac Traxler
   17  *    Date:
   18  * Purpose: fun
   19  * Problem: 11389
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_VALUES 10005
   27 
   28 int morning[MAX_VALUES];
   29 int afternoon[MAX_VALUES];
   30 int day;
   31 int routes;
   32 int overtimeRate;
   33 
   34 int compareAsc(const void *a, const void *b)
   35 {
   36     /* FUNCTION compare */
   37     return ( *(int*)a - *(int*)b );
   38 } /* FUNCTION compare */
   39 
   40 int compareDsc(const void *b, const void *a)
   41 {
   42     /* FUNCTION compare */
   43     return ( *(int*)a - *(int*)b );
   44 } /* FUNCTION compare */
   45 
   46 void init()
   47 {
   48     /* FUNCTION init */
   49 } /* FUNCTION init */
   50 
   51 void dump()
   52 {
   53     /* FUNCTION dump */
   54 } /* FUNCTION dump */
   55 
   56 int getInput()
   57 {
   58     /* FUNCTION getInput */
   59     int dataReadFlag;
   60     int i;
   61 
   62     scanf(" %d %d %d ", &routes, &day, &overtimeRate);
   63     dataReadFlag = (0 != day) && (0 != routes) && (0 != overtimeRate);
   64     if (dataReadFlag)
   65         {
   66             /* load routes */
   67             for (i=0; i<routes; i++)
   68                 {
   69                     scanf(" %d ", &morning[i]);
   70                 }
   71             for (i=0; i<routes; i++)
   72                 {
   73                     scanf(" %d ", &afternoon[i]);
   74                 }
   75         } /* load routes */
   76     return (dataReadFlag);
   77 } /* FUNCTION getInput */
   78 
   79 void process()
   80 {
   81     /* FUNCTION process */
   82     int over = 0;
   83     int tot;
   84     int i;
   85 
   86     qsort(morning, routes, sizeof(int), compareAsc);
   87     qsort(afternoon, routes, sizeof(int), compareDsc);
   88     for (i=0; i<routes; i++)
   89         {
   90             /* for each pairing */
   91             if ((morning[i] + afternoon[i]) > day)
   92                 {
   93                     /* overtimeRate to be paid */
   94                     over = over + morning[i] + afternoon[i] - day;
   95                 } /* overtimeRate to be paid */
   96         } /* for each pairing */
   97     tot = over * overtimeRate;
   98     printf("%d\n", tot);
   99 } /* FUNCTION process */
  100 
  101 int main()
  102 {
  103     /* main */
  104     int moreToDo;
  105 
  106     init();
  107     moreToDo = getInput();
  108     while (moreToDo)
  109         {
  110             /* while */
  111             process();
  112             moreToDo = getInput();
  113         } /* while */
  114 
  115     return EXIT_SUCCESS;
  116 } /* main */
  117