Editorial for LCC '24 Contest 2 J3 - Eric's Apple Orchard


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

Author: Moonlight

Subtask 1

Note that when X \geq N or X \leq 0, Eric's profit is non-positive. He would just make more money by not selling apples.

For this subtask, it suffices to try all values of X from 1 to N-1, and take the first value of X that maximizes the profit.

Subtask 2

Note that Eric's revenue can be written as -X^2+Nx. By completing the square, we get -(X-\frac{N}{2})^2+\frac{N^2}{4}.

When N is even, then the profit is simply maximized if and only if X = \frac{N}{2}. However, when N is odd, then the minimum value of this expression is \frac{N^2 - 1}{4}, attained when |X-\frac{N}{2}| = \frac{1}{2}. In other words, this occurs when X = \frac{N \pm 1}{2}. Taking the smaller value, the solution is \frac{N-1}{2}.


Comments

There are no comments at the moment.