Editorial for Back To School '19: Unstable Books


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

Please note that since Ninjaclasher refused to write an editorial, wleung_bvg decided to write his own version. His solution is slightly different than intended.

Subtask 1

For the first subtask, we can see that for each pair of indices i and Ni+1, there are only two options: swapped, and not swapped. Since there are N2 of these pairs (where i<Ni+1), then there are 2N2 possible permutations of the books. For each permutation, we can count the minimum number of increasing and decreasing subsections using a greedy algorithm to extend the current subsection as long as possible in O(N) time. We keep track of the minimum and print that as the answer.

Time Complexity: O(N2N2)

Subtask 2

This is a reminder that wleung_bvg still does not know how to dp, so please bear with him again.

For the second subtask, we can use dynamic programming to efficiently compute the answer. We will store the optimal answer for the subarrays [1,i] and [Ni+1,N] for all integers 1iN2. A key observation is that to compute the optimal answer, we only need to worry about the previous/next 2 elements in the array, in addition to the current element.

Let us define a function c(a,b,c). If b is either the maximum or minimum of {a,b,c}, then c(a,b,c)=1. Otherwise, c(a,b,c)=0. If a,b,c are consecutive heights on the shelf, we can see that this corresponds to a change from either increasing to decreasing, or decreasing to increasing, which increases the instability of the shelf.

For simplicity, we can define another function f(i,inv). If inv=1, then f(i,inv)=hNi+1, otherwise, f(i,inv)=hi.

Let dp[i][j][k][m] (1iN and 0j,k,m1) store the minimum instability for the subarray [1,i] and [Ni+1,N] where index i is swapped with Ni+1 iff j=1, i1 is swapped with Ni iff k=1, and index i2 is swapped with Ni1 iff m=1.

We can see the optimal answer for each state relies on the optimal answer ending at index i1 with the same elements swapped as the current state. The optimal answer for the current state is at least min(dp[i1][k][m][0],dp[i1][k][m][1]).

We know that if there is a change in direction for f(i,j),f(i1,k),f(i2,m), then the optimal answer increases by one. This also applies for f(i,1j),f(i1,1k),f(i2,1m). Thus, we have the following:

dp[i][j][k][m]=min(dp[i1][k][m][0],dp[i1][k][m][1])+c(f(i,j),f(i1,k),f(i2,m))+c(f(i,1j),f(i1,1k),f(i2,1m))

When N is even, the optimal answer should be the minimum of dp[N2][j][k][m] for all 0j,k,m1, however the answer could possibly be off by 1 depending on the number of direction changes not counted between indices N2 and N2+1. Consider c(f(N2,j),f(N2+1,j),f(N2+2,k)) and c(f(N21,j),f(N2,j),f(N2+1,k)). If these two values are both 0, then it means that the subarray [N21,N2+2] belongs to the same subsection, and that dp[N2][j][k][m] is 1 greater than the actual answer. If both values are 1, then [N2,N2+1] forms an additional subsection that has not been counted and that dp[N2][j][k][m] is 1 less than the actual answer. In all other cases, dp[N2][j][k][m] stores the correct answer for those specific values of j,k,m. We can keep track of the minimum for all 0j,k,m1 and print the answer.

It is very likely that one of the dimensions in the dynamic programming can be eliminated. Unfortunately wleung_bvg doesn't know how and since Ninjaclasher refused to write the editorial, this is what you get.

Time Complexity: O(N)

Subtask 3

For the third subtask, we can compute the dp array in the same way as subtask 2, however we have to handle the adjustment at the end differently. Consider c(f(N21,k),f(N2,j),f(N2+1,k)). If this value is 0, then [N21,N2+1] belongs to the same subsection, and dp[N2][j][k][m] is 1 greater than the actual answer. Otherwise, the subsection formed by that change in direction has already been counted during the dynamic programming. Once again, the answer is the minimum of dp[N2][j][k][m] for all 0j,k,m1.

Time Complexity: O(N)


Comments

There are no comments at the moment.