Editorial for JOI '16 Open P3 - Skyscraper
Submitting an official solution before solving the problem yourself is a bannable offence.
Subtask 1 [5 points]
Brute force. Try every permutation.
Subtask 2 [15 points]
Deciding each of the numbers from the leftmost position.
This can be solved by using bit DP. We calculate , where is the set of used numbers in the array, is the last chosen number, and is the current total sum of all differences of two adjacent numbers.
From each DP state, you can choose one of the unused numbers, so the total time complexity will be .
Subtask 3 [80 points]
In this Subtask, you should decide the relative positions of each number from the smallest value.
In other words, you should choose positions from below (see Figure 1).
This can be solved by DP. We consider , where means we calculate the number of ways to put the -th smallest element, is the number of connected components, is the current total cost, and is the number of elements already used for the leftmost or the rightmost edges. The treatment of connected components is tricky. When you add the -th smallest element, either (1) it is disconnected with other connected components, (2) it is connected with one connected component only, or (3) it connects two connected components.
The additional cost on each phase depends on the value (after sorting ), and the number of connected components.
For further description about this DP, see AppleTrees from Member SRM 489, Seatfriends from SRM 625, or SumOverPermutations from SRM 666.
Comments