Thank you all for participating in the round.
A
Author: RobinFromTheHood Developer: Filikec
This problem requires a simple implementation. Set a variable (initially $$$0$$$) to represent the gold Robin has, and update it according to the rules as he scan through $$$a_i$$$, adding $$$1$$$ to answer whenever Robin gives away a gold.
B
Author: ChairmanFMao Developer: Filikec, RobinFromTheHood
The key observation is that $$$i^i$$$ has the same even/odd parity as $$$i$$$. Therefore, the problem reduces to finding whether the sum of $$$k$$$ consecutive integers ending in $$$n$$$ is even. This can be done by finding the sum of $$$n-k+1, n-k+2, ..., n-1, n$$$ which is $$$k*(2n-k+1)/2$$$, and checking its parity. Alternatively, one can count the number of odd numbers in those $$$k$$$ consecutive integers.
Originally, the number of leaves was to be $$$i^m$$$ according to the fractal nature of life (e.g. https://www.science.org/doi/10.1126/science.284.5420.1677), where $$$m$$$ is some integer. Developers decided to replace $$$m$$$ with $$$i$$$ for simplicity, following Filikec's suggestion.
C
Author: RobinFromTheHood Developer: Filikec, RobinFromTheHood
If we sort the wealth in increasing order, then the j-th person must be unhappy for Robin to appear, where $$$j=\floor{n/2}+1$$$ if $$$1$$$-indexing or $$$j=\floor{n/2}$$$ if $$$0$$$-indexing. We need $$$w_j < \frac{s+x}{2*n}$$$, where $$$s$$$ is the original total wealth before $$$x$$$ gold from the pot was added. Rearranging the equation gives $$$x>2*n*w_j-s$$$. Because $$$x$$$ is a non-negative integer, we arrive at the answer $$$max(0,2*n*w_j-s+1)$$$.
Of course, this problem can also be solved by binary search, with two caveats. First, one needs to be careful to avoid comparison between integer and float types, as rounding errors could create issues. Second, one needs to pick the upper limit carefully to ensure it is large enough.
There are $$$2$$$ edge cases, $$$n=1, 2$$$, where the condition for Robin can never be reached, because the richest person will always be happy (at least in this problem, though perhaps not IRL). ChatGPT struggled to identify these edge cases, so it was tempting to leave at least one hidden. In the end we decided to give both in samples to reduce frustration. This problem was originally intended for B, but was moved to C after tester feedback.
D
Author: RobinFromTheHood Developer: Filikec, ChairmanFMao
Since the number of days $$$n$$$ is capped, we can check all possible start day $$$x$$$ in range $$$[1,n-d+1]$$$ (so that the duration of $$$d$$$ days would fit). We would like to find the number of overlapped jobs for each value of $$$x$$$.
A job between days $$$l_i$$$ and $$$r_i$$$ would overlap with the visit if the start day $$$x$$$ satisfies $$$l_i-d+1 \le x \le r_i$$$. Naively, this range update could be potentially $$$O(n)$$$, which is too slow. However, noting the start and end, each job update could be done in $$$2$$$ operations. We add $$$+1$$$ at $$$l_i-d+1$$$ and $$$-1$$$ at $$$r_i+1$$$, and after all jobs are recorded, we will take a prefix sum to work out the number of overlapped jobs for each $$$x$$$. When $$$l_i-d+1$$$ drops below $$$1$$$, we simply use $$$1$$$ to avoid lower values which are not being considered for x.
The time complexity is $$$O(n)$$$.
E
Author: RobinFromTheHood Developer: Filikec, RobinFromTheHood
This problem builds on the standard Dijkstra algorithm. So please familiarise yourself with the algorithm if not already.
In Dijkstra algorithm, a distance vector/list is used to store travel times to all vertices, here we need to double the vector/list to store travel times to vertices arriving with and without a horse. If a vertex has a horse, then it's possible to transition from without horse to with horse there. The Dijkstra algorithm is then run as standard.
What if a horse has already been taken by Marian when Robin arrives, and vice versa? Well, the optimal solution would not require the second to arrive to use the horse, because the first to arrive could simply wait for the second to arrive, giving an earlier meeting than whatever is possible if the second to arrive had to use the horse to go elsewhere. Therefore, for any vertex, $$$1$$$ horse is sufficient.
We run Dijkstra algorithm twice to find the fastest time Robin and Marian could reach any vertex $$$i$$$ as $$$TR(i)$$$ and $$$TM(i)$$$. The earliest meeting time at a given vertex $$$i$$$ is $$$max(TR(i),TM(i))$$$, and we need to check all vertices.
The time complexity is that of Dijkstra algorithm which, in this problem, is $$$O(n \log n)$$$.
G
Author: RobinFromTheHood Developer: Filikec, RobinFromTheHood
The key for this problem is the use of a stack, where last item in is the first item out. As we scan through the diary entries, we will only drink till the day of the next entry. If there is left over milk, we will push them into the stack with number of pints and the day they were acquired. If there isn't enough milk to reach the next entry, we will check the stack for left overs. Careful implementation is required to check for expiry day. It might help to append a fictitious entry with large day number and $$$0$$$ pints. Since every pop from the stack accompanies either the processing of a diary entry or permanently removing a stack item, the number of stack operation is $$$O(n)$$$.
Originally, this problem has an easy version, where Little John drinks the oldest drinkable milk first. However testers and the team were uncertain about the difficulties of the two problems, and there was concern that they are too `implementation heavy'. For the sake of balance, only the hard version is presented here as G. However, you may wish to try the easy version yourself. If you ever wondered about the difference of opening the freshest or the oldest milk in the fridge, this is the chance!
Since diary entries are presented in sorted order, the time complexity is $$$O(n)$$$.