Editorial for WC '17 Contest 2 S4 - One Does Not Simply Walk Into Mordor


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.

We will first consider the case in which the line segments may intersect. Let's define a "region" as a contiguous sequence of equal L values (making sure to count L_N and L_1 as being consecutive). Let r be the number of regions (which can be easily counted in \mathcal O(N) time), and note that r is guaranteed to be at least 2. There are then r points on the circle which are immediately surrounded by two different L values, meaning that at least one line segment must pass through the circle there. This suggests that there must be at least r line segment endpoints, and so at least c = \lceil \frac r 2 \rceil line segments. Furthermore, c line segments are always enough to get the job done. Imagine sorting the r points mentioned above, adding one more arbitrary point if r is odd, and then connecting the first point to the c-th one, the second one to the (c+1)-st one, and so on. This will divide up the circle such that each region is segmented off entirely from the others.

The non-intersecting case isn't so simple, but can be solved with dynamic programming. Let DP[i][s] be the minimum number of line segments required to divide up the portion of the circle starting at index i and spanning s markings, with L_i being the portion's "representative marking". We'd like to consider various ways of cutting this portion into smaller sub-portions while saving on cuts when those sub-portions have markings matching L_i. Let's consider all possible splits of this subarray into two subarrays, with the first including the first k markings and the second including the remaining s-k markings (1 \le k < s). Note that the second subarray starts at marking i+k (wrapping around if it exceeds N).

If k > 1, we'll make one cut to separate index i from the remainder of the first subarray, which will then require DP[i+1][k-1] more cuts itself. Similarly, we'll make one more cut to separate out the second subarray, which will then require DP[i+k][s-k] more cuts itself. However, each of these two main cuts can be omitted if that portion's representative marking (L_{i+1} or L_{i+k}) is equal to L_i. We can calculate DP[i][s] by determining the above value for all possible values of k and taking the minimum.

Finally, any arbitrary marking can be used as the "representative marking" of the entire circle, meaning that the answer is for example DP[1][N]. The approach described above involves \mathcal O(N^2) DP states and \mathcal O(N) transitions per state, resulting in a time complexity of \mathcal O(N^3).


Comments

There are no comments at the moment.