Editorial for DMOPC '19 Contest 7 P2 - Dinner Party


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: Tzak

Consider the problem on a line instead of around a circle, so the 0-th family member is no longer adjacent to the (n-1)-th family member. Now, we serve the family members one-by-one. The 0-th member must be served a_0 dishes, and the only way to do so is to serve the 0-th and 1-st members simultaneously. If in doing so, we serve the 1-st member too many dishes, there is no solution, because we cannot satisfy the 0-th and 1-st members simultaneously: we either have to feed the 1-st member too many dishes, or otherwise not serve the 0-th member enough. If we can satisfy the 0-th family member, we then must satisfy the 1-st member by feeding the 1-st and 2-nd member simultaneously, and the problem continues for each subsequent i-th and (i+1)-th member for each i<N-1.

Now, we return to the original problem: what if we have the option to serve the 0-th family member adjacent to the (n-1)-th family member k times before proceeding with the problem as if it were in a line? To do this, we can brute force all possible combinations of k in the range [0, \min(a_0, a_{n-1})] for \mathcal O(N\min(a_0,a_{n-1})) complexity, which in the worst case takes 5 \times 10^{11} operations.

To achieve a better complexity, observe that the problem is the same given any cyclic shift of a. If we can find the minimum element, and then shift a such that the minimum element resides at a_0, we can achieve \mathcal O(N + N\min(a_i)) complexity. Although this is deceivingly similar to our previous complexity, we see that \min(a_i) is at most \frac{\sum a_i}{N}, so N \min(a_i) \le N \frac{\sum a_i}{N} = \sum a_i, meaning that our complexity is equivalent to \mathcal O(N + \sum a_i)!

Alternatively, we can represent the problem as a system of equations with odd N and even N cases to solve the problem in \mathcal O(N), however printing the output costs an additional \mathcal O(N + \sum a_i) and we see that in both the complexity is equivalent to \mathcal O(N + \sum a_i) anyways.

Pseudocode:

(returns original index before shifting)
function shifted(i)
    return (i + idx) mod n

(checks if there is answer for some k)
function possible(k)
    tmp is copy of a
    tmp[0] -= k
    tmp[N - 1] -= k
    for i in [0, N - 1)
        tmp[i + 1] -= tmp[i]
        if tmp[i + 1] < 0
            return false
    return tmp[N - 1] == 0

(prints answer for some k)
function print_answer(k)
    print "YES"
    print sum / 2
    do k times
        print shifted(0), shifted(n - 1)
    for i in [0, N - 1)
        do a[i] times
            a[i + 1]--
            print shifted(i), shifted(i + 1)

read N, a
sum = sum of all elements in a

idx is index of minimum element in a
shift a such that a[idx] is first element

for k in [0, min(a_0, a_(n-1))]
    if possible(k):
        print_answer(k)
        terminate

print "NO"

Time complexity: \mathcal O(N + \sum a_i)


Comments

There are no comments at the moment.