Computer Programming Contest Preparation

ToolBox - Source for: 123/12356/a.c



/home/toolbox/public_html/solutions/123/12356/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-23
   18  * Purpose: fun
   19  * Problem: 12356 - Army Buddies
   20  */
   21 
   22 /*
   23  * This template reads data until a terminating value is reached.
   24  */
   25 
   26 #define MAX_SOLDIERS 100002
   27 #define DEAD -1
   28 
   29 int leftSoldiers[MAX_SOLDIERS];  /* who is to left of each soldier */
   30 int rightSoldiers[MAX_SOLDIERS]; /* who is to right of each soldier */
   31 int S;  /* how many soldiers */
   32 int B;  /* number of loss reports */
   33 
   34 void init(int s)
   35 {
   36     /* FUNCTION init */
   37     int i;
   38 
   39     for (i=1; i<=s; i++)
   40         {
   41             /* for */
   42             leftSoldiers[i] = i - 1;
   43             rightSoldiers[i] = i + 1;
   44         } /* for */
   45     leftSoldiers[1] = DEAD;
   46     rightSoldiers[s] = DEAD;
   47 } /* FUNCTION init */
   48 
   49 void dump()
   50 {
   51     /* FUNCTION dump */
   52 } /* FUNCTION dump */
   53 
   54 int getInput()
   55 {
   56     /* FUNCTION getInput */
   57     int dataReadFlag;
   58 
   59     scanf(" %d %d ", &S, &B);
   60     dataReadFlag = (0 != S) || (0 != B);
   61     return (dataReadFlag);
   62 } /* FUNCTION getInput */
   63 
   64 void kill(int left, int right)
   65 {
   66     /* FUNCTION kill */
   67     int i;
   68 
   69     /* when soldiers 2-5 are killed, (in the left list), the soldier that was to
   70      * the right of 5 needs to now point to the soldier that was to the left of 2
   71      * L[R[5]] = L[2].
   72      */
   73 
   74     leftSoldiers[rightSoldiers[right]] = leftSoldiers[left];
   75     rightSoldiers[leftSoldiers[left]] = rightSoldiers[right];
   76 
   77 } /* FUNCTION kill */
   78 
   79 void printLeft(int x)
   80 {
   81     /* FUNCTION printLeft */
   82     if (DEAD != leftSoldiers[x])
   83         {
   84             printf("%d", leftSoldiers[x]);
   85         }
   86     else
   87         {
   88             printf("*");
   89         }
   90 } /* FUNCTION printLeft */
   91 
   92 void printRight(int x)
   93 {
   94     /* FUNCTION printRight */
   95     if (DEAD != rightSoldiers[x])
   96         {
   97             printf("%d", rightSoldiers[x]);
   98         }
   99     else
  100         {
  101             printf("*");
  102         }
  103 } /* FUNCTION printRight */
  104 
  105 void process()
  106 {
  107     /* FUNCTION process */
  108     int i;
  109     int d1;
  110     int d2;
  111 
  112     init(S);
  113     for (i=0; i<B; i++)
  114         {
  115             /* for each loss report */
  116             scanf(" %d %d ", &d1, &d2);
  117             printLeft(d1);
  118             printf(" ");
  119             printRight(d2);
  120             printf("\n");
  121             kill(d1, d2);
  122         } /* for each loss report */
  123     printf("-\n");
  124 } /* FUNCTION process */
  125 
  126 int main()
  127 {
  128     /* main */
  129     int moreToDo;
  130 
  131     moreToDo = getInput();
  132     while (moreToDo)
  133         {
  134             /* while */
  135             process();
  136             moreToDo = getInput();
  137         } /* while */
  138 
  139     return EXIT_SUCCESS;
  140 } /* main */
  141