Computer Programming Contest Preparation

ToolBox - Source for: 112/11292/a.c



/home/toolbox/public_html/solutions/112/11292/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 #include <ctype.h>
   10 
   11 #define TRUE  (1 == 1)
   12 #define FALSE (1 != 1)
   13 
   14 #define DEBUG if (FALSE)
   15 
   16 /*
   17  *  Author: Isaac Traxler
   18  *    Date: 2021-11-19
   19  * Purpose: fun
   20  * Problem: 11292 - The Dragon of Loowater
   21  */
   22 
   23 /*
   24  * This template reads data until a terminating value is reached.
   25  */
   26 
   27 #define MAX_CASES 20001
   28 
   29 int knightCnt;
   30 int knights[MAX_CASES];
   31 int headCnt;
   32 int heads[MAX_CASES];
   33 
   34 void init()
   35 {
   36     /* FUNCTION init */
   37 } /* FUNCTION init */
   38 
   39 void dump()
   40 {
   41     /* FUNCTION dump */
   42 } /* FUNCTION dump */
   43 
   44 int cmpfunc (const void * a, const void * b)
   45 {
   46     /* FUNCTION cmpfunc */
   47     return ( *(int*)a - *(int*)b );
   48 } /* FUNCTION cmpfunc */
   49 
   50 int getInput()
   51 {
   52     /* FUNCTION getInput */
   53     int dataReadFlag = FALSE;
   54     int i;
   55 
   56     scanf(" %d %d ", &headCnt, &knightCnt);
   57     if ((0 != headCnt) || (0 != knightCnt))
   58         {
   59             /* another case to process */
   60             dataReadFlag = TRUE;
   61             for (i=0; headCnt>i; i++)
   62                 {
   63                     scanf(" %d ", &heads[i]);
   64                 }
   65             for (i=0; knightCnt>i; i++)
   66                 {
   67                     scanf(" %d ", &knights[i]);
   68                 }
   69         } /* another case to process */
   70     return (dataReadFlag);
   71 } /* FUNCTION getInput */
   72 
   73 void process()
   74 {
   75     /* FUNCTION process */
   76     int k;
   77     int h;
   78     int canSkip;
   79     int skipped;
   80     int total;
   81 
   82     if (heads > knights)
   83         {
   84             /* not enough knights */
   85             printf("Loowater is doomed!\n");
   86         } /* not enough knights */
   87     else
   88         {
   89             /* must see if the knights are tall enough */
   90             qsort(heads, headCnt, sizeof(int), cmpfunc);
   91             qsort(knights, knightCnt, sizeof(int), cmpfunc);
   92             skipped = 0;
   93             canSkip = knightCnt - headCnt;
   94             /* go through list started with smallest of both
   95              * if knight is not tall enough, skip him
   96              * if we skip so many knights that not enough remain, we bail
   97              * if knight is tall enough, add height to total
   98              */
   99             k = 0; /* knight counter */
  100             h = 0; /* head counter */
  101             total = 0; /* cost */
  102             while ((headCnt>h) && (skipped<=canSkip))
  103                 {
  104                     /* while */
  105                     if (knights[k] >= heads[h])
  106                         {
  107                             /* knight is tall enough */
  108                             total = total + knights[k];
  109                             k++;
  110                             h++;
  111                         } /* knight is tall enough */
  112                     else
  113                         {
  114                             /* knight is not tall enough -- skip him */
  115                             k++;
  116                             skipped++;
  117                         } /* knight is not tall enough -- skip him */
  118                 } /* while */
  119             if (skipped > canSkip)
  120                 {
  121                     /* ran out of knights */
  122                     printf("Loowater is doomed!\n");
  123                 } /* ran out of knights */
  124             else
  125                 {
  126                     /* present the bill */
  127                     printf("%d\n", total);
  128                 } /* present the bill */
  129 
  130         } /* must see if the knights are tall enough */
  131 } /* FUNCTION process */
  132 
  133 int main()
  134 {
  135     /* main */
  136     int moreToDo;
  137 
  138     init();
  139     moreToDo = getInput();
  140     while (moreToDo)
  141         {
  142             /* while */
  143             process();
  144             moreToDo = getInput();
  145         } /* while */
  146 
  147     return EXIT_SUCCESS;
  148 } /* main */
  149