[Problem solving summary] Atcoder Beginner Contest summary

ABC 178 F Contrast

Question : Given two lengths as NNNon-descendingAA of NA andBBB , ask if you can rearrangeBBB 使得 ∀ i , A i ≠ B i \forall i, A_i \neq B_i i,Ai=Bi. Give a plan if you can.

Method one (fake practice)

No solution is easy to determine, as long as there is a certain number in A, BA, BA,The sum of the number of occurrences in B is greater thanNNN has no solution.

Later I thought of a fake method: put all the numbers that have appeared in accordance with their A, BA, BA,The sum of the number of occurrences in B is arranged in descending order, and then the numbers are put into the sequence to be constructed in this order. Put priority on those inA, BA, Beach timeA,The number with the highest sum of occurrences in B is inAAThe position that appears in A. This can be maintained with set.

It is possible that it will be unsolvable afterwards. My approach is to first construct the sequence regardless of whether there is no solution, and finally find the illegal positions, and then randomly exchange positions. Finally, if all the exchanges are successful, there will be a solution.

Method Two

Flip BB directlyB , Illegal position and swap the two sides.

Guess you like

Origin blog.csdn.net/zqy1018/article/details/108167389