Computer Programming Contest Preparation

ToolBox - Source for: 1/105/a.c



/home/toolbox/public_html/solutions/1/105/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 /*
   17  *  Author: Isaac Traxler
   18  *    Date: 2014-03-12
   19  * Purpose:
   20  * Problem: 105
   21  */
   22 
   23 /*
   24  * This template reads lines of data at a time until end of file.
   25  */
   26 
   27 #define CITY_WIDTH 10001
   28 
   29 int city[CITY_WIDTH];
   30 int minCity = CITY_WIDTH;
   31 int maxCity = 0;
   32 
   33 void init()
   34 {
   35     /* FUNCTION init */
   36     int i;
   37 
   38     for (i=0; i<CITY_WIDTH; i++)
   39         {
   40             /* for */
   41             city[i] = 0;
   42         } /* for */
   43 } /* FUNCTION init */
   44 
   45 void dump()
   46 {
   47     /* FUNCTION dump */
   48 } /* FUNCTION dump */
   49 
   50 int getInput()
   51 {
   52     /* FUNCTION getInput */
   53     int dataReadFlag = FALSE;
   54     int i;
   55     int left;
   56     int height;
   57     int right;
   58 
   59     if (3 == scanf(" %d %d %d ", &left, &height, &right))
   60         {
   61             /* found data */
   62             DEBUG printf("Input: %d %d %d\n", left, height, right);
   63             dataReadFlag = TRUE;
   64             if (minCity > left)
   65                 {
   66                     minCity = left;
   67                 }
   68             if (maxCity < right)
   69                 {
   70                     maxCity = right;
   71                 }
   72             for (i=left; i<right; i++)
   73                 {
   74                     /* set each height */
   75                     if (height > city[i])
   76                         {
   77                             city[i] = height;
   78                         }
   79                 } /* set each height */
   80         } /* found data */
   81     /* for (i=minCity; i<=maxCity; i++) { printf("%2d ", city[i]); } printf("\n"); */
   82     return (dataReadFlag);
   83 } /* FUNCTION getInput */
   84 
   85 void process()
   86 {
   87     /* FUNCTION process */
   88     int i;
   89     int prev = -1;
   90     int cnt = 1;
   91     int first = TRUE;
   92 
   93     /* for (i=minCity; i<(2+maxCity); i++) {  printf("%2d ", city[i]); }   printf("\n"); */
   94     for (i=minCity; i<=maxCity; i++)
   95         {
   96             /* for */
   97             if (prev != city[i])
   98                 {
   99                     /* altitude change */
  100                     if (! first)
  101                         {
  102                             printf(" ");
  103                         }
  104                     first = FALSE;
  105                     printf("%d %d", i, city[i]);
  106                     prev = city[i];
  107                     cnt = 0;
  108                 } /* altitude change */
  109             else
  110                 {
  111                     /* same height */
  112                     cnt++;
  113                 } /* same height */
  114         } /* for */
  115     printf("\n");
  116 } /* FUNCTION process */
  117 
  118 int main()
  119 {
  120     /* main */
  121     int moreToDo;
  122 
  123     init();
  124     moreToDo = getInput();
  125     while (moreToDo)
  126         {
  127             /* while */
  128             moreToDo = getInput();
  129         } /* while */
  130     process();
  131 
  132     return EXIT_SUCCESS;
  133 } /* main */
  134