Editorial for DMOPC '21 Contest 8 P3 - Weaker Data
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.
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
Hint
Insert the numbers in the order . How many new valleys are added each time?Hint 2
Use dynamic programming.Solution
Consider inserting the numbers in the order . If we are inserting the number and there are previously inserted numbers before , this adds new valleys. We can now use dynamic programming, where represents that we have inserted numbers and there are currently valleys. As we may have valleys in the worst case, we need states and a total of transitions.To solve for the lexicographically smallest permutation, we can let be a list, representing the lexicographically smallest partially formed permutation. Comparing these permutations adds another factor of to the complexity, but this solution should still easily pass subtask 1 as the constant factors are tiny.
Time Complexity: or
Non Lexicographically Least
Hint
Which are possible?Hint 2
Use a greedy algorithm.Subtask 2 Solution
Consider which are possible for each . Trying some small cases, following our approach of inserting in order, we can find that:- When or , only is possible.
- When , we can add either or to a number obtainable from . Thus are possible.
- When , we can add either or to a number obtainable from . Thus are possible.
- When , we can add either or or to a number obtainable from . Thus are possible.
Proof
Let for all be the maximum possible . We will prove by induction on that all between and can be formed this way. The base cases are clear. Suppose the induction hypothesis is true for . Then we have two cases:- If , then the greedy algorithm will not increase , so is still when there are numbers left.
- If , then note that , so the greedy algorithm will leave as when there are numbers left.
Time Complexity:
Subtask 3 Optimizations
For subtask 3, we can binary search for each time and use a data structure such as a binary indexed tree to simulate the greedy algorithm.Time Complexity:
Lexicographically Least
Hint
Print out all the lexicographically smallest permutations for small generated by a brute force or the subtask 1 algorithm, and look for patterns.Hint 2
Except for a single family of corner cases that reduce to , we should always insert a number either at the front or in the middle of the partially formed permutation.Subtask 2 Solution
Printing out all the lexicographically smallest permutations for small , we might notice some patterns. In general, we should always insert a number at the left end if possible (to minimize the first element of the permutation), or we should insert it in the middle (intuitively, this leaves as small as possible for the rest of the permutation, which makes it more likely that the first element of the final permutation is small). This is true except for when the permutation reduces to the case, where the permutation should be2 1 4 3 5
.
More precisely, let for all and let for all . We should insert at the left end if , or insert at position if , except for the case, and continue to fill out similarly.
Proof Sketch
Let be a -indexed sequence of length , the lexicographically least permutation for the inputN K
, defined for all . For any two sequences and , let denote that is lexicographically smaller than , let denote the sequence formed by adding 1 to all terms of , and let denote the sequence formed by inserting the number before in .
We will prove by induction on that will be generated by this algorithm, and furthermore, it satisfies for all . Small base cases of or so can be computer verified with the subtask 1 dynamic programming code.
Suppose that and the induction hypothesis is true for . Then for some . Since replacing by gives the same , we must in fact have . Now we have some cases:
- If , then the algorithm inserts at the beginning of . Thus, must be , and it follows that and the algorithm indeed correctly generates .
- If , then the algorithm inserts at the middle of , before position . We must have ; suppose that .
- If , then we have by induction hypothesis. It suffices to prove that and first differ in a position less than .
Note that . In the algorithm, the numbers that get inserted in the beginning while forming a sequence of the form is equivalent to the sequence from applying a greedy subset sum algorithm to with (that is, loop through in order. Whenever we see a number that does not exceed the current value of , add to the front of the sequence and subtract from ), up to some small issues near the end from the case.
Note that for all . It follows that if is the first number that did not get subtracted from (because ), then each time some with is subtracted from , is reduced to at most half the old value. Since , we can apply this lemma with and (using ) to conclude that at most numbers get inserted in the beginning while forming and . As , these sets of numbers are different. Furthermore, we can show that no two numbers (except for the ones that are almost ) in these sets differ by , so when the first elements of the partially formed permutations differ, this difference will only get "pushed" right by elements inserted at the beginning. It follows that there is indeed a difference within the first numbers. - Otherwise, . It suffices to prove that .
By the algorithm, is equal to minus the largest such that and is equal to minus the largest such that . As , . This means that . Hence as required.
Finally, note that for , because , and for , so for all as required.
Therefore by induction, the correctness of this algorithm is proven. - If , then we have by induction hypothesis. It suffices to prove that and first differ in a position less than .
Time Complexity:
Subtask 3 Optimizations
For subtask 3, we can simulate the above algorithm in linear time using two double-ended queues to store the first and second halves of the partially formed permutation. Whenever the first half grows too large, we move its rightmost element to the left side of the second half.Time Complexity:
Comments