Codeforces 1148E Earth Wind and Fire

analysis

Necessary conditions:
① $ \ sum_. 1 = {I}} ^ {n-S_I = \ sum_. 1 = {I}} ^ {n-$ t_i

Pretreatment:
The $ s, t $ from small to large.

Try a
end to end match. Examples of
S = 2, 2,. 4,. 7,. 9
T =. 4,. 5,. 5,. 5,. 5

4, 2, 4, 7, 7
4, 4, 4, 7, 5
4, 5, 4, 6, 5
4, 5, 5, 5, 5

Anti Example
S =. 1,. 4,. 5,. 8
T = 2,. 3,. 6,. 7

key observation:
Consider sorted $ s $ and $ t $.
① Every operation is bound to make a $ s_i $ become a $ t_j $.
② solution if then there is a method of operating such that the relative order of the number of the last s constant. In other words, the last $ s_1 $ becomes $ t_1 $, $ s_2 $ becomes $ t_2 $ ......

You may be required to prove that the second conclusion. Assuming that there is a beginning $ s_i <s_j $ last $ s_i> s_j $, then consider the $ s_i <s_j $ becomes $ s_i> s_j $ of that operation, assuming that this operation is to make $ s_i $ increases, this operation can be split into two: first $ s_i $ becomes $ s_j $, then $ s_j $ becomes $ s_i $ target.

So we can put this issue as a problem of matching brackets.
If the $ s_i <t_i $ put $ t_i - s_i $ regarded as "left bracket", if $ s_i> t_i $ put $ s_i - t_i $ regarded as "right parenthesis."
If $ s_i <t_i $ put tuple $ (t_i - s_i, \ text {$ s_i $ original subscript}) $ into the stack encounter "right parenthesis" will be the top of the stack with the "left brackets "pairing, if the stack is left bracket" depleted "which will pop the stack; if the stack is empty and" right parenthesis "it indicates that there is a surplus no solution.

Official explanations

Code

Guess you like

Origin www.cnblogs.com/Patt/p/11865691.html