Editorial for CCC '21 S3 - Lunch Concert


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

Subtask 1

Observe that the only field positions that can be optimal are between the leftmost and rightmost positions of all friends, so we can simply check those positions.

Time Complexity: O(N×max(Pi))

Subtask 2

We use the same observation as the last subtask except we must check positions more quickly, which we can do with linesweep.

Consider how the answer changes as we move from position x to x+1: for every i where Pi+Dix, we must add Wi to the answer, and for every j where x<PjDj, we must subtract Wj from the answer. Thus, we simply need to maintain the sum of weights on the left and right of the current position.

Note: At any position x, let leftSum=i=1,xPi+DiNWi and rightSum=i=1,x<PiDiNWi.

This solution can also be used to explain why field positions between the leftmost and rightmost positions of all friends are optimal: if we are to the left of the leftmost position of a friend, leftSum=0 while rightSum>0, so moving to the right will always result in a better answer. A similar argument can be used to prove the same for positions to the right of the rightmost position of a friend.

Time Complexity: O(N+max(Pi))

Subtask 3

The solution from subtask 2 can be extended by realizing that an optimal answer will be at Pi±Di for some 1iN (we'll call these positions candidate positions). We can prove this by considering the leftSum and rightSum variables again: leftSum and rightSum only change at candidate positions, meaning that if we're between two candidate positions, we can always move left (if leftSumrightSum) or right (if leftSumrightSum) until we reach a candidate position for a better answer.

Time Complexity: O(NlogN)

Alternative Solution

Let fi(x) be the walking time of the ith friend if the concert was at position x.

fi(x)={Wi×(PiDix)if x<PiDi0if PiDixPi+DiWi×(xPiDi)if x>Pi+Di

We can prove that fi(x) is convex for all 1iN using one of the conditions for convex functions:

A function f is convex if for all 0t1 and x1,x2X (where X is the domain of f), f(tx1+(1t)x2)tf(x1)+(1t)f(x2). Geometrically, this means that if we draw a line between (x,f(x)) and (y,f(y)) for any x,y in f's domain, it must sit above the graph of f. A bit of drawing on a sheet of paper and observing how fi looks like a straightened out parabola can help convince us that it satisfies the condition. However, we can also prove this formally by considering the 6 possible cases of which piece of fi (fi is piecewise) x1 and x2 fall on.

To complete our solution, observe that as the sum of convex functions is also convex, F(x)=f1(x)+f2(x)++fN(x) is convex as well. Thus we can binary search for the first point where F(x) changes from decreasing to non-decreasing, which will be our answer.

Evaluating fi(x) for any i,x takes O(1) time, so evaluating F(x) takes O(N) time.

Time Complexity: O(NlogPi)


Comments


  • 2
    TheBoss123  commented on Jan. 10, 2022, 6:15 a.m. edited

    What data structure would be best to keep track of P, W, and D? (C++)

    Also is sorting necessary in this problem?


  • 2
    DynamicSquid  commented on March 11, 2021, 3:40 a.m.

    For the second subtask, how do we check if a person is on the left of right of a position x? Would that require us to iterate over each person every time we move position x?


    • 2
      Overseas  commented on March 11, 2021, 4:12 p.m.

      binary search,


      • 2
        DynamicSquid  commented on March 12, 2021, 3:11 a.m.

        I tried binary search, but I can't get it to work. What am I doing wrong?


        • 2
          noirsnow  commented on March 22, 2021, 12:45 a.m.

          I first iterate over each person once to calculate and record the spots that weight changes when passing. Then I just loop through the line while checking the spots. This is enough to get through subtask 2.