Codeforces Round #848 (Div. 2) AE game-time thinking + correct solution

Qingda Konjac for the first time in the official div 2 div2A c Acin d i v 2A c has five questions, and it is also the best performance of the little konjac in history. After this, my cf may also turn yellow.

A. Flip Flop Sum
Question meaning: given a - ia - ia —— i array, the weight is only 1 or -1, we select two adjacent bits and flip them (1 to -1, -1 to 1), what is the maximum sum that can be obtained, n <= 2 e 5n <= 2e5n<=2 e 5 .
Idea: Violently enumerate all adjacent bits and flip them to find the maximum sum.
the code

B. The Forbidden Permutation
title meaning: Given a length of nnn permutation arrayppp , gives you the length inmmArray aa with m and different elementsa , an array is bad if and only if all1 <= i < m 1 <= i < m1<=i<m满足 p o s ( a i ) < p o s ( a ( i + 1 ) ) < p o s ( a i ) + d pos(a_i) < pos(a_(i_+1_)) < pos(a_i) + d pos(ai)<pos(a(i+1))<pos(ai)+d p o s ( x ) pos(x) p os ( x ) meansxxx value ataaThe position of the a array, the following operations can now be performed, we can exchangeppAdjacent items of the p array, how many operations can we perform to makeaaa array is good,n <= 2 e 5 , m <= 2 e 5 , ai <= nn <= 2e5 , m <= 2e5 , a_i <= nn<=2 e 5 ,m<=2e5ai<=n .
The idea is to destroy the condition of the bad array. We found that what he requires is that all adjacent positions must be satisfied. We consider a minimum cost that is not satisfied. Consider two cases, consider a ( i + 1) a(i+1)a(i+1 ) move toa ( i ) a(i)a ( i ) front ora ( i + 1 ) a(i+1)a(i+1 ) move toa(i) + da(i) + da(i)+After d , the cost can go to the minimum value.
the code

C. Flexible String
Meaning of the question: Give you two lengths of nnthe string aaof na b b b , we define the value as( l , r ) (l , r)(l,r ) for the number of satisfying requirements( l <= r , l >= 1 , r <= n ) (l <= r , l >= 1 , r <= n)(l<=r,l>=1,r<=n ) , satisfy the requirement if and only ifa ( l , r ) = = b ( l , r ) a(l , r) == b(l , r)a(l,r)==b(l,r ) The substrings from l to r of string a are the same as the substrings from l to r of string b.
We define aQQQ stack, we can do the following, we can putaaai a_i of a stringaiPut it on QQIn the Q pile, then putai a_iaiReplace with any character ccc, Q Q The number of different strings in the Q heap cannot exceed kkK types, find the maximum value of the string after several operations, the title guarantees the stringaaa b b The type of strings that appear in b does not exceed 10, k <= 10 , n <= 2 e 5 k <= 10 , n <= 2e5k<=10,n<=2 e 5 .
Idea: Consider binary enumeration of 10, enumerate all legal states, and find the maximum value of the string corresponding to the state. Time complexityO ( 2 k ∗ n ) O(2 ^k * n)O(2kn )
code

D. Flexible String Revisit
title meaning: give you two lengths of nnthe binary stringaa of na b b b , the value of the string is only0, 1 0, 10 , 1 , we can randomly select a subscriptiii , afterai aiai inversion(0 becomes 1, 1 becomes 0) (0 becomes 1, 1 becomes 0)( 0 to 1 , 1 to 0 ) , ask the expected number of operations required to make the two strings the same.

Idea: consider transforming the problem and turn the problem into ai aiai sumbi biThe same number of bi is kkk , different numbers can be deduced asn − k nknk .
Then we putai aiai sumbi biThe same number of bi is kkk is called statekkk , we consider the statekkk to statennThe expected operation of n dp [ k ] dp[k]d p ​​[ k ] times.
1. One operation, we choseai aiai sumbi biThe subscript in bi is the same, because it was the same before, it becomes different after inversion, then the state is transferred to the statek − 1 k-1k1 .
2. One operation, we choseai aiai sumbi biThe subscripts in bi are not the same. Due to the inversion, the different subscripts become the same subscripts and shift to the statek + 1 k+1k+1 .
It can be deduced that the formula is: dp [ k ] = 1 + k / n ∗ dp [ k − 1 ] + ( n − k ) / n ∗ dp [ k + 1 ] dp[k] = 1 + k/n * dp [k - 1] + (nk)/n * dp[k+1]dp[k]=1+k/ndp[k1]+(nk)/ndp[k+1 ] Take a look at this formula, we found that dp [ k − 1 ] and dp [ k + 1 ] dp[k - 1] and dp[k + 1] are
out of the formuladp[k1 ] and d p [ k+1 ] , such a formula cannot find the answer.

We consider transforming the angle, we consider from state kkk to statek+1 k+1k+1 desired desired operationdp [ k ] dp[k]d p ​​[ k ] times, the answer isdp [ k ] dp[k]d p ​​[ k ] frommmm ton − 1 n-1nThe sum of 1 , the cause ismmm tonnn , we first spenddp [ m ] dp[m]d p ​​[ m ] becomesm + 1 m+1m+1 , then spenddp [ m + 1 ] dp[m+1]dp[m+1 ] becomesm+2 m+2m+2 ... and so on costdp [ n − 1 ] dp[n-1]dp[n1 ] tonnn .
Consider state transitions.
1. One operation, we choseai aiai sumbi biThe subscripts in bi are not the same. Due to the inversion, the different subscripts become the same subscripts and shift to the statek + 1 k+1k+1 , the number of operations is 1.
2...Operate once, we chooseai aiai sumbi biThe subscript in bi is the same, because it was the same before, it becomes different after inversion, then the state is transferred to the statek − 1 k-1k1 , we start from statek − 1 k-1k1 becomesk+1 k+1k+1 needs to go throughdp [ k − 1 ] dp[k-1]dp[k1 ] operation becomes statekkk , then go throughdp [ k ] dp[k]d p ​​[ k ] operations become statek + 1 k+1k+1 , the number of operations is1 + dp [ k − 1 ] + dp [ k ] 1 + dp[k - 1] + dp[k]1+dp[k1]+d p ​​[ k ] .
The formula becomes: dp [ k ] = ( n − k ) / n + k / n ∗ ( 1 + dp [ k − 1 ] + dp [ k ] ) dp[k] = (n - k)/n + k/n * (1 + dp[k - 1] + dp[k])dp[k]=(nk)/n+k/n(1+dp[k1]+d p ​​[ k ])
considering simplification, the simplification process is as follows:
insert image description here

The final formula becomes dp [ k ] = n / ( n − k ) + k / ( n − k ) ∗ dp [ k − 1 ] dp[k] = n / (n - k) + k / (n - k) * dp[k - 1]dp[k]=n/(nk)+k/(nk)dp[k1 ]
Just transfer this formula, the initial valuedp [ 0 ] = 1 dp[0]=1dp[0]=1 , because the transfer from 0 to 1 must be one time, because the same one is empty, you can only choose from different ones, and then findm − ( n − 1 ) m - (n -1)m(n1) d p [ k ] dp[k] d p ​​[ k ] and can be.
the code

E. The Tree Has Fallen!
Title: Give you a tree with a size of nnA tree of n , each point has valueai aiai , withqqq times of inquiry, each inquiry starts withrrr is the root, inuuSelect several points in the u subtree so that the weight XOR sum of these points is the largest, and output the largest XOR sum.
Idea: consider going offline, we turn all inquiries into questionsrrr for root,uuThe form of the largest XOR sum of the u subtree, and then we passdfs dfsdf s , first passdfs dfsdf s maintains sz[u] sz[u]rooted at 1Maximum XOR sum of sz [ u ] (linear basis), second pass dfs dfsdf s is similar to rootdp dpThe idea of ​​dp is maintained byuuu is the maximum XOR sum (linear basis) of other node subtrees rooted at u, and then processes all queries offline.
Principle: The only difference between all maintained subtree information with two subtrees as the root is the path information between the two subtrees (the direction of the rooted tree is different), and then we can consider maintaining the information of 1 subtree first, Then usedfs dfsThe df s feature (which can maintain the passing path information) can solve the problem.
The graphics and text of the principle will be updated in the future to facilitate understanding. Linear base merging is the basic content, so I won’t go into details here for the time being.
the code

Guess you like

Origin blog.csdn.net/qq_52358098/article/details/128848628