Editorial for WC '17 Contest 2 S4 - One Does Not Simply Walk Into Mordor
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 values (making sure to count and as being consecutive). Let be the number of regions (which can be easily counted in time), and note that is guaranteed to be at least . There are then points on the circle which are immediately surrounded by two different values, meaning that at least one line segment must pass through the circle there. This suggests that there must be at least line segment endpoints, and so at least line segments. Furthermore, line segments are always enough to get the job done. Imagine sorting the points mentioned above, adding one more arbitrary point if is odd, and then connecting the first point to the -th one, the second one to the -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 be the minimum number of line segments required to divide up the portion of the circle starting at index and spanning markings, with 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 . Let's consider all possible splits of this subarray into two subarrays, with the first including the first markings and the second including the remaining markings . Note that the second subarray starts at marking (wrapping around if it exceeds ).
If , we'll make one cut to separate index from the remainder of the first subarray, which will then require more cuts itself. Similarly, we'll make one more cut to separate out the second subarray, which will then require more cuts itself. However, each of these two main cuts can be omitted if that portion's representative marking ( or ) is equal to . We can calculate by determining the above value for all possible values of 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 . The approach described above involves DP states and transitions per state, resulting in a time complexity of .
Comments