Computer Programming Contest Preparation

ToolBox - Source for: 6/661/a.c



/home/toolbox/public_html/solutions/6/661/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: 2017-01-22
   18  * Purpose: fun
   19  * Problem: 661 Blowing Fuses
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_DEVICES 22
   27 #define OFF 0
   28 #define ON 1
   29 
   30 int n;  /* number of devices */
   31 int m;  /* number of operations */
   32 int c;  /* fuse capacity */
   33 
   34 int devices[MAX_DEVICES];
   35 int caps[MAX_DEVICES];
   36 
   37 void init()
   38 {
   39     /* FUNCTION init */
   40 } /* FUNCTION init */
   41 
   42 void dump()
   43 {
   44     /* FUNCTION dump */
   45 } /* FUNCTION dump */
   46 
   47 int getInput()
   48 {
   49     /* FUNCTION getInput */
   50     int dataReadFlag;
   51 
   52     scanf(" %d %d %d ", &n, &m, &c);
   53     dataReadFlag = (m>0) || (n>0) || (c>0);
   54     return (dataReadFlag);
   55 } /* FUNCTION getInput */
   56 
   57 void loadCap()
   58 {
   59     /* FUNCTION loadCap */
   60     int i;
   61 
   62     for (i=1; i<=n; i++)
   63         {
   64             /* for each device */
   65             scanf(" %d ", &caps[i]);
   66             devices[i] = OFF;  /* set all devices off */
   67         } /* for each device */
   68 } /* FUNCTION loadCap */
   69 
   70 void process()
   71 {
   72     /* FUNCTION process */
   73     int i;
   74     int tot = 0;
   75     int dv;
   76     int blown = FALSE;
   77     int mx = 0;
   78 
   79     loadCap();
   80     for (i=0; i<m; i++)
   81         {
   82             /* process each operation */
   83             scanf(" %d ", &dv);
   84             switch (devices[dv])
   85                 {
   86                     /* switch */
   87                 case OFF:
   88                     devices[dv] = ON;
   89                     tot = tot + caps[dv];
   90                     blown = blown || (tot > c);
   91                     if (tot > mx)
   92                         {
   93                             mx = tot;
   94                         }
   95                     break;
   96                 case ON:
   97                     devices[dv] = OFF;
   98                     tot = tot - caps[dv];
   99                     break;
  100                 } /* switch */
  101         } /* process each operation */
  102     if (blown)
  103         {
  104             printf("Fuse was blown.\n");
  105         }
  106     else
  107         {
  108             printf("Fuse was not blown.\n");
  109             printf("Maximal power consumption was %d amperes.\n", mx);
  110         }
  111 } /* FUNCTION process */
  112 
  113 int main()
  114 {
  115     /* main */
  116     int moreToDo;
  117     int cnt = 1;
  118 
  119     moreToDo = getInput();
  120     while (moreToDo)
  121         {
  122             /* while */
  123             printf("Sequence %d\n", cnt);
  124             cnt++;
  125             process();
  126             printf("\n");
  127             moreToDo = getInput();
  128         } /* while */
  129 
  130     return EXIT_SUCCESS;
  131 } /* main */
  132