Computer Programming Contest Preparation

ToolBox - Source for: naq/2026/l.c



/home/toolbox/public_html/solutions/naq/2026/l.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 #include <ctype.h>
   10 
   11 #define TRUE  (1 == 1)
   12 #define FALSE (1 != 1)
   13 
   14 #define DEBUG if (FALSE)
   15 
   16 /* fprintf(stderr, "functionName: message", varslist); */
   17 
   18 /*
   19  *  Author: Isaac Traxler
   20  *    Date: 2026-10-03
   21  * Purpose: fun
   22  * Problem: l - Stardust Curry
   23  */
   24 
   25 /*
   26  * This template reads data a specified number of times.
   27  */
   28 
   29 #define MAXBREAKS 100009
   30 
   31 int numberOfTimes;
   32 
   33 /*
   34  * sum = sum up durations of breaks
   35  * base = pushups / sum
   36  * tot = 0
   37  * for each time
   38  *   t = duration (t)
   39  *   tot = tot + (base * t) * (base * t) / t
   40  *     which simplifies to:
   41  *   tot = tot + base * base * t
   42  *
   43  * tot is answer
   44  *
   45  */
   46 
   47 double tot;
   48 int durCnt;
   49 int dur[MAXBREAKS];
   50 int sum;
   51 double base;
   52 double base2;
   53 double pushups;
   54 
   55 void init()
   56 {
   57     /* FUNCTION init */
   58     scanf("%d ", &numberOfTimes);
   59 } /* FUNCTION init */
   60 
   61 void dump()
   62 {
   63     /* FUNCTION dump */
   64 } /* FUNCTION dump */
   65 
   66 void getInput()
   67 {
   68     /* FUNCTION getInput */
   69     int i;
   70 
   71     scanf(" %d %lf ", &durCnt, &pushups);
   72     sum = 0;
   73     for (i=0; i<durCnt; i++)
   74         {
   75             /* get each duration */
   76             scanf(" %d ", &dur[i]);
   77             sum = sum + dur[i];
   78         } /* get each duration */
   79     base = pushups / sum;
   80     base2 = base * base;
   81     DEBUG printf("(pushups %lf) (base %d)\n", pushups, base);
   82 } /* FUNCTION getInput */
   83 
   84 void process()
   85 {
   86     /* FUNCTION process */
   87     int i;
   88 
   89     tot = 0;
   90     for (i=0; i<durCnt; i++)
   91         {
   92             /* for each time */
   93             tot = tot + base2 * dur[i];
   94             DEBUG printf("(i %d) (base %lf) (base2 %lf) (dur[%d] %d) {tot %lf)\n", i, base, base2, i, dur[i], tot);
   95         } /* for each time */
   96     printf("%lf\n", tot);
   97 } /* FUNCTION process */
   98 
   99 int main()
  100 {
  101     /* main */
  102     int i;
  103 
  104     init();
  105     for (i=0; i<numberOfTimes; i++)
  106         {
  107             /* while */
  108             getInput();
  109             process();
  110         } /* while */
  111 
  112     return EXIT_SUCCESS;
  113 } /* main */
  114 
  115