Computer Programming Contest Preparation

ToolBox - Source for: 131/13109/a.c



/home/toolbox/public_html/solutions/131/13109/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: 2018-10-31
   20  * Purpose: fun
   21  * Problem: 13109
   22  */
   23 
   24 /*
   25  * This template reads data a specified number of times.
   26  */
   27 
   28 #define MAX_ELEPHANTS 100001
   29 
   30 int numberOfTimes;
   31 long tot;
   32 int elephants[MAX_ELEPHANTS];
   33 int cnt;
   34 long weight;
   35 
   36 void init()
   37 {
   38     /* FUNCTION init */
   39     scanf("%d ", &numberOfTimes);
   40 } /* FUNCTION init */
   41 
   42 void dump()
   43 {
   44     /* FUNCTION dump */
   45 } /* FUNCTION dump */
   46 
   47 void getInput()
   48 {
   49     /* FUNCTION getInput */
   50     int i;
   51 
   52     scanf(" %d %ld ", &cnt, &weight);
   53     for (i=0; cnt>i; i++)
   54         {
   55             /* get each weight */
   56             scanf(" %d ", &elephants[i]);
   57         } /* get each weight */
   58 
   59 } /* FUNCTION getInput */
   60 
   61 int compare(const void *a, const void *b)
   62 {
   63     /* FUNCTION compare */
   64     return ( *(int*)a - *(int*)b );
   65 } /* FUNCTION compare */
   66 
   67 void process()
   68 {
   69     /* FUNCTION process */
   70     int i;
   71 
   72     qsort(elephants, cnt, sizeof(int), compare);
   73     tot = 0;
   74     for (i=0; ((tot + elephants[i]) < weight) && (i<cnt); i++)
   75         {
   76             /* sum up weights */
   77             tot = tot + elephants[i];
   78             DEBUG printf("(tot %ld) (weight %ld) (elephant[%d] = %d)\n", tot, weight, i, elephants[i]);
   79         } /* sum up weights */
   80     printf("%d\n", i);
   81 } /* FUNCTION process */
   82 
   83 int main()
   84 {
   85     /* main */
   86     int i;
   87 
   88     init();
   89     for (i=0; i<numberOfTimes; i++)
   90         {
   91             /* while */
   92             getInput();
   93             process();
   94         } /* while */
   95 
   96     return EXIT_SUCCESS;
   97 } /* main */
   98 
   99