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.

Author: zhouzixiang2004

Subtask 1

Hint Insert the numbers in the order N,N1,,1. How many new valleys are added each time?
Hint 2 Use dynamic programming.
Solution Consider inserting the numbers in the order N,N1,,1. If we are inserting the number x and there are i previously inserted numbers before x, this adds i(Nxi) new valleys. We can now use dynamic programming, where dp[i][j] represents that we have inserted i numbers and there are currently j valleys. As we may have O(N3) valleys in the worst case, we need O(N4) states and a total of O(N5) transitions.

To solve for the lexicographically smallest permutation, we can let dp[i][j] be a list, representing the lexicographically smallest partially formed permutation. Comparing these permutations adds another factor of N to the complexity, but this solution should still easily pass subtask 1 as the constant factors are tiny.

Time Complexity: O(N5) or O(N6)

Non Lexicographically Least

Hint Which K are possible?
Hint 2 Use a greedy algorithm.
Subtask 2 Solution Consider which K are possible for each N. Trying some small cases, following our approach of inserting N,N1,,1 in order, we can find that:
  • When N=1 or N=2, only K=0 is possible.
  • When N=3, we can add either 0 or 1 to a number obtainable from N=2. Thus K[0,1] are possible.
  • When N=4, we can add either 0 or 2 to a number obtainable from N=3. Thus K[0,3] are possible.
  • When N=5, we can add either 0 or 3 or 4 to a number obtainable from N=3. Thus K[0,7] are possible.
At this point, we might realize that the problem reduces to representing K as the sum of numbers of the form i(Nxi), and we might conjecture that all K between 0 and the maximum possible K are obtainable. Let's try a greedy approach to write K in this form. Since the added numbers are smaller when inserting N,N1,, we should intuitively use them last to gain more control over K. This encourages us to loop through x in the order 1,2,,N, greedily picking the largest i(Nxi) that does not exceed the current value of K each time.
Proof Let ax=x2x2 for all x be the maximum possible i(xi). We will prove by induction on N that all K between 0 and n=0N1an can be formed this way. The base cases N=1,2 are clear. Suppose the induction hypothesis is true for N1. Then we have two cases:
  • If Kn=0N2an, then the greedy algorithm will not increase K, so K is still n=0N2an when there are N1 numbers left.
  • If n=0N2an<Kn=0N1an, then note that aN1(n=0N2an)+1K, so the greedy algorithm will leave K as KaN1n=0N2an when there are N1 numbers left.
In both cases, the induction step follows.
Afterwards, it is easy to simulate inserting the numbers in quadratic time overall.

Time Complexity: O(N2)
Subtask 3 Optimizations For subtask 3, we can binary search for i each time and use a data structure such as a binary indexed tree to simulate the greedy algorithm.

Time Complexity: O(NlogN)

Lexicographically Least

Hint Print out all the lexicographically smallest permutations for small N 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 N=5,K=4, we should always insert a number x 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 N, 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 K 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 N=5,K=4 case, where the permutation should be 2 1 4 3 5.

More precisely, let ax=x2x2 for all x and let bn=x=0n1ax for all n. We should insert 1 at the left end if 0KbN1, or insert 1 at position N+12 if bN1<KbN, except for the N=5,K=4 case, and continue to fill out 2,3,,N similarly.
Proof Sketch Let Lex(N,K) be a 0-indexed sequence of length N, the lexicographically least permutation for the input N K, defined for all 0KbN. For any two sequences p and q, let p<q denote that p is lexicographically smaller than q, let p+1 denote the sequence formed by adding 1 to all terms of p, and let Ins(p,c,x) denote the sequence formed by inserting the number x before pc in p.

We will prove by induction on N that Lex(N,K) will be generated by this algorithm, and furthermore, it satisfies Lex(N,k)<Lex(N,k+1) for all 0k<bN. Small base cases of 1N<100 or so can be computer verified with the subtask 1 dynamic programming code.

Suppose that N100 and the induction hypothesis is true for N1. Then Lex(N,K)=Ins(Lex(N1,Kc(N1c))+1,c,1) for some 0cN1. Since replacing c by N1c gives the same Kc(N1c), we must in fact have 0cN12. Now we have some cases:
  • If 0KbN1, then the algorithm inserts 1 at the beginning of Lex(N1,K)+1. Thus, Lex(N,K)0 must be 1, and it follows that c=0 and the algorithm indeed correctly generates Lex(N,K).
  • If bN1<KbN, then the algorithm inserts 1 at the middle of Lex(N1,K)+1, before position N12. We must have c>0; suppose that c<N12.

    • If clog2(aN1)+5, then we have Lex(N1,KaN1)<Lex(N1,Kc(N1c)) by induction hypothesis. It suffices to prove that Lex(N1,KaN1) and Lex(N1,Kc(N1c)) first differ in a position less than c.

      Note that Kc(N1c)>KaN1>bN1aN1. In the algorithm, the numbers that get inserted in the beginning while forming a sequence of the form Lex(N1,bN1k) is equivalent to the sequence from applying a greedy subset sum algorithm to k with aN2,aN3,,a0 (that is, loop through aN2,aN3,,a0 in order. Whenever we see a number ai that does not exceed the current value of k, add N1i to the front of the sequence and subtract ai from k), up to some small issues near the end from the N=5,K=4 case.

      Note that ai12ai+1 for all i. It follows that if aj is the first number that did not get subtracted from k (because k<aj), then each time some ai with i<j is subtracted from k, k is reduced to at most half the old value. Since Kc(N1c)>KaN1>bN1aN1, we can apply this lemma with k=bN1(Kc(N1c))<aN1 and k=bN1(KaN1)<aN1 (using aj=aN1) to conclude that at most log2(aN1)+2 numbers get inserted in the beginning while forming Lex(N1,KaN1) and Lex(N1,Kc(N1c)). As KaN1Kc(N1c), these sets of numbers are different. Furthermore, we can show that no two numbers (except for the ones that are almost N) in these sets differ by 1, 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 c numbers.
    • Otherwise, c<log2(aN1)+5. It suffices to prove that Lex(N1,KaN1)0<Lex(N1,Kc(N1c))0.

      By the algorithm, Lex(N1,KaN1)0 is equal to N1 minus the largest i such that aibN1(KaN1) and Lex(N1,Kc(N1c))0 is equal to N1 minus the largest j such that ajbN1(Kc(N1c)). As N100, aN1(N1)214>(log2(aN1)+5)(N1(log2(aN1)+5))+N2>c(N1c)+N2. This means that ai+1>bN1(KaN1)>bN1(Kc(N1c))+N2aj+N2>aj+1i>j. Hence Lex(N1,KaN1)0=N1i<N1j=Lex(N1,Kc(N1c))0 as required.


    Finally, note that Lex(N,k)=Ins(Lex(N1,k)+1,0,1)<Ins(Lex(N1,k+1),0,1)=Lex(N,k+1) for 0k<bN1, Lex(N,bN1)<Lex(N,bN1+1) because Lex(N,bN1)0=1<Lex(N,bN1+1)0, and Lex(N,k)=Ins(Lex(N1,kaN1)+1,N12,1)<Ins(Lex(N1,k+1aN1)+1,N12,1)=Lex(N,k+1) for bN1+1K<bN, so Lex(N,k)<Lex(N,k+1) for all 0k<bN as required.

    Therefore by induction, the correctness of this algorithm is proven.
It is easy to simulate inserting the numbers in quadratic time overall.

Time Complexity: O(N2)
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: O(N)

Comments

There are no comments at the moment.