Editorial for UHCC1 P3 - Busy Elevator


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

For this problem, you are given an array of positive integers, w_1,w_2,\dots,w_n, and a positive integer L. You can change the position of at most one element in the array to maximize the M such that w_1+w_2+\dots+w_M\leq L.

Let M be the initial maximum (i.e. without changing the position of any element, either (w_1+w_2+\dots+w_M\leq L and w_1+w_2+\dots+w_M+w_{M+1}> L) or M=n), and let i be the index of the element you wish to change position, j be the index of this element after changing its position, w_1',w_2',\dots,w_n' be the new array. Consider the following four situations:

Case 1: if 1\leq i\leq M, and 1\leq j\leq M, then w_1'+w_2'+\dots+w_M'=w_1+w_2+\dots+w_M\leq L as we just reordered the first M elements, therefore this doesn't improve our answer.

Case 2: if 1\leq i\leq M, and M<j\leq n, then it is optimal to pick the i with maximum w_i and move it to the back of the array.

Case 3: if M<i\leq n, and 1\leq j\leq M, then it is optimal to pick the i with minimum w_i and move it to the front of the array.

Case 4: if M<i\leq n, and M<j\leq n, then moving any i with i>M+1 to positions >M+1 would have no effect on the answer, moving it to positions \leq M+1 would reduce the problem to case 3. Now consider moving i=M+1, as it potentially might be large and block the entrance to the elevator, so we move it to the back of the array.


Comments

There are no comments at the moment.