Editorial for UTS Open '21 P6 - Terra Mater


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

Binary search the minimum danger factor D that requires K or fewer operations to enforce.

Instead of finding the minimum number of hills that must be adjusted, find the maximum number of hills that can be left alone. The height difference between each consecutive hill is at most D. So if hill i and hill j are both unadjusted, |h[i]h[j]|D(ij).

Partial Solution

Let dp[i]= the maximum number of unadjusted hills up to hill i, assuming you don't adjust hill i. Let j be the last hill before i that was unadjusted. dp[i]=max(dp[j])+1 for all |h[i]h[j]|D(ij). In total there can be max1iNdp[i] unadjusted hills. This gives an O(N2log109) solution, which doesn't give any points on DMOJ because this subtask doesn't exist.

Full Solution

The condition |h[i]h[j]|D(ij) can be converted to h[i]h[j]D(ij),h[j]h[i]D(ij), so you are trying to find the longest sequence of hills a1,a2, such that Daih[ai]Dai+1h[ai+1] and Dai+h[ai]Dai+1+h[ai+1]. This is equivalent to finding the longest nondecreasing subsequence over pairs (Dih[i],Di+h[i]) for 1iN which results in an O(NlogNlog109) solution.


Comments

There are no comments at the moment.