
2023 · Task 3
Elves
Every year in the run-up to Christmas, the elves in the big factory at the North Pole have to pack the presents coming off numerous production lines and stack them in piles. This year most of them are helping Santa with LaTeX functions, so the ones left behind have to work hard, but also smart.
Over the course of the day, each production line produces presents at a different rate, described by a polynomial of degree 4 (ax4+bx3+cx2+dx+e), where x denotes the time in hours (0-24, a real number). The number of presents produced in a given interval equals the area under the polynomial over that interval (from 0 to 24 the polynomials only take non-negative values). The number of presents is rounded down to an integer. If 4.3 presents were produced in an interval, the elf would collect only the 4 fully finished ones.
An elf can only be at one production line at any given moment, and moving to another line and getting ready to work takes 6 minutes, i.e. 0.1 hours. Elves may also share a line, but then they collect the same number of presents as if only one of them were there.
The 200 elves now need to be cleverly distributed across the 1000 production lines to maximise the number of presents collected in a day. Help them with this challenge.
Hint for secondary school students: have a little look at integrals (and computing integrals in code). 😉
Input
1000 lines, each with 5 real numbers representing the coefficients of a polynomial, where the 1st number is the coefficient of x4. E.g. 1 2 3 4 5 represents x4+2x3+3x2+4x+5
Output
For each of the 200 elves, print how many times it changes production line (the number S), followed by S+1 lines of output. Each of these S+1 lines contains two numbers x and y. x is the 0-indexed number of the production line, and y is the time the elf spends on it. E.g.
2
0 5
1 4.8
2 14
means that the elf changes production line twice: it starts its working day on line 0 and spends 5 hours there, then it is on line 1 from 5.1 to 9.9 hours, and on line 2 from 10 until the end of the day.
Example
Input
Suppose we had one elf and the following 2 functions (see the picture below):
- 0x4 + 0.018x3 - 0.848x2 + 9.641x + 19.963 (red)
- -0.001x4 + 0.048x3 - 0.483x2 + 0.75x + 22 (green)
The input would be:
0 0.018 -0.848 9.641 19.963
-0.001 0.048 -0.483 0.75 22
Output
The elf could be on the red line for the first 14.8 hours, and then on the green one from 14.9 to 24 (9.1 hours). In that case the output would be:
1
0 14.8
1 9.1
And it would collect 650 + 584 = 1234 presents.

Solution
You can try out and test solutions by hand here (instructions are included - README.md).