Computer Programming Contest Preparation

ToolBox - Source for: 126/12608/a.c



/home/toolbox/public_html/solutions/126/12608/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 /* fprintf(stderr, "functionName: message", varslist); */
   16 
   17 /*
   18  *  Author: Isaac Traxler
   19  *    Date: 2015-10-03
   20  * Purpose: fun
   21  * Problem: 12608 - Garbage Collection
   22  */
   23 
   24 /*
   25  * This template reads data a specified number of times.
   26  */
   27 
   28 int numberOfTimes;
   29 int weightLimit;
   30 int stops;
   31 
   32 void init()
   33 {
   34     /* FUNCTION init */
   35     scanf("%d ", &numberOfTimes);
   36 } /* FUNCTION init */
   37 
   38 void dump()
   39 {
   40     /* FUNCTION dump */
   41 } /* FUNCTION dump */
   42 
   43 void getInput()
   44 {
   45     /* FUNCTION getInput */
   46     scanf(" %d %d ", &weightLimit, &stops);
   47 } /* FUNCTION getInput */
   48 
   49 void process()
   50 {
   51     /* FUNCTION process */
   52     int i;
   53     int tot = 0;
   54     int distance;
   55     int weight;
   56     int currentWeight = 0;
   57 
   58     for (i=0; i<stops; i++)
   59         {
   60             /* for each stop */
   61             scanf(" %d %d ", &distance, &weight);
   62             currentWeight = currentWeight + weight;
   63             if (currentWeight > weightLimit)
   64                 {
   65                     /* to heavy -- must empty */
   66                     currentWeight = weight;
   67                     tot = tot + distance + distance;
   68                 } /* to heavy -- must empty */
   69             if (currentWeight == weightLimit)
   70                 {
   71                     /* empty this load */
   72                     tot = tot + distance + distance;
   73                     currentWeight = 0;
   74                 } /* empty this load */
   75             DEBUG printf("i=%d  tot=%d  distance=%d  weight=%d  currentWeight=%d\n", i, tot, distance, weight, currentWeight);
   76         } /* for each stop */
   77     if (0 < currentWeight)
   78         {
   79             tot = tot + distance + distance;
   80         }
   81     printf("%d\n", tot);
   82 } /* FUNCTION process */
   83 
   84 int main()
   85 {
   86     /* main */
   87     int i;
   88 
   89     init();
   90     for (i=0; i<numberOfTimes; i++)
   91         {
   92             /* while */
   93             getInput();
   94             process();
   95         } /* while */
   96 
   97     return EXIT_SUCCESS;
   98 } /* main */
   99 
  100