#P8197. Beautiful Queue Formation
Beautiful Queue Formation
Beautiful Queue Formation
In this problem, there are n people standing in a line. Each person has a height denoted by \(a_i\). You are given a target formation represented by a sequence \(b\) of length \(n\). The formation is considered beautiful if and only if for every \(i=1,2,\dots,n\) the condition \(a_i=b_i\) holds.
You are allowed to swap only two adjacent people. Moreover, the total number of adjacent swaps must not exceed \(n^2\) operations.
Your task is to determine whether it is possible to transform the initial formation into the beautiful formation. If it is possible, you need to output YES
followed by a valid sequence of swap operations. Otherwise, output NO
.
Note: When a swap operation is performed, you swap the person at position \(i\) with the one immediately after him (i.e. position \(i+1\)). The positions are 1-indexed.
inputFormat
The input consists of three lines:
- The first line contains a single integer \(n\) (the number of people).
- The second line contains \(n\) integers \(a_1,a_2,\dots,a_n\), representing the heights in the initial formation.
- The third line contains \(n\) integers \(b_1,b_2,\dots,b_n\), representing the target (beautiful) formation.
outputFormat
If it is impossible to reach the target formation by performing at most \(n^2\) adjacent swaps, output a single line containing NO
.
If it is possible, output YES
in the first line, followed by a line containing an integer \(k\) (the number of swaps performed). Each of the next \(k\) lines should contain a single integer \(p\) (1-indexed) indicating that the people at positions \(p\) and \(p+1\) are swapped. The total number of swap operations \(k\) must not exceed \(n^2\).
sample
3
1 2 3
1 2 3
YES
0
</p>