We need to solve: given target array a[1..n] and initial array b[1..n], both contain same multiset of numbers (some duplicates). We can swap adjacent elements. Need to output any sequence of adjacent swaps (each swap: positions i and i+1) that transforms b into a. Number of swaps <= 1e6. n <= 300.
We can simulate bubble sort like process: bring each element from left to right to its target position. Since we have duplicate values, we need to map which occurrence goes where. The guarantee that it's possible means there exists a permutation pi such that b[pi] = a[i] (same heights). Actually we need to reorder b to match a. Since duplicates, there might be many such permutations. We can pick any that works.
A simple approach: iterate i from 1 to n, if b[i] != a[i], then find some index j > i such that b[j] == a[i] (the element we need at i). Then we can bring it to position i by repeatedly swapping adjacent (swap j-1 and j, then j-2 and j-1, ...). This is like moving the element leftwards. This yields at most n^2 swaps (since each move reduces distance by 1). For n=300, n^2=90k, far below 1e6. So it's safe.
But careful: there may be duplicates, so when we pick j we need to ensure that we pick an occurrence that will not disturb the already placed ones? Since we are moving leftwards, it's fine. However, there is a subtlety: after moving the element to position i, the relative order of other elements (excluding those already fixed) might change, but we don't care as long as eventually we match a.
But does this always produce the correct target? Let's test: we fix positions from left to right. At step i, we ensure that b[1..i] = a[1..i]. Since we only move elements from positions > i into i, and we only do swaps among elements beyond i, the prefix is not disturbed after we set it? Actually when we move an element from j to i by swapping leftwards, we are swapping it with the elements in positions i,...,j-1. Those elements shift right by one. Since we have already fixed positions 1..i-1, moving the element leftwards only swaps with elements that are currently at positions i... j-1, which are not yet fixed (since we are about to set position i). After we finish, b[i] becomes the desired a[i], and the elements that were shifted right maintain order? Actually they get shifted right by one: the element that was at i moves to i+1, etc. But the relative order among those shifted elements remains as they were? Example: b = [x, y, z, w]; we want a[1]=w, and w is at j=4. Move left: swap 3-4: [x,y,w,z]; swap 2-3: [x,w,y,z]; swap 1-2: [w,x,y,z]. Now prefix w matches a[1]. But notice that the other elements (x,y,z) are now in the order (x,y,z)? Actually we started [x,y,z,w] and after swapping we got [w,x,y,z]. So the order among the elements originally at positions 1..3 (x,y,z) got shifted right, but their relative order among themselves remains x,y,z? They appear as x,y,z after the moves. So yes, relative order among the elements we haven't yet fixed (including those that were shifted) is preserved. That's good because we only care about final order matching a, and we'll later adjust the rest.
Thus greedy works: for i=1..n, if b[i] != a[i], find the smallest j > i such that b[j] == a[i]? Actually we need to find some j such that b[j] == a[i]. Since we will bring it leftwards, any j works. But to be safe about duplicates, we should pick the one that will eventually lead to the correct ordering. Does it matter? Since the multiset is the same, and we process left to right, we need to assign each occurrence of a value in a to a corresponding occurrence in b. If we pick any occurrence at step i, could it cause an impossibility later? Consider duplicates: a = [2,2,1], b = [1,2,2]. At i=1, we need a1=2. b[2]=2 and b[3]=2. If we pick j=2, then move left: swap 1-2 => b becomes [2,1,2]. Now prefix matches. i=2: need a2=2, b[2]=1 !=2. Find j>2: b[3]=2, move left: swap 2-3 => [2,2,1] which matches. Works. If we pick j=3 initially: at i=1, need 2 from position 3: move left via swaps: swap 2-3 => [1,2,2]; swap 1-2 => [2,1,2] same as before. So both work.
But there could be cases where picking the "wrong" duplicate leads to mismatch later? Possibly if we have more than two duplicates and the ordering of duplicates matters. But because the values are identical, any assignment of occurrences should be fine? Actually if the values are identical, swapping two identical values does nothing. But the target order a might have duplicate values, and the exact identity of each student with same height might not matter because heights are the only attribute? The problem statement says: "ai is the height of the i-th student in the line". There are students with same heights. They are distinct individuals but only identified by height? Usually in such problems, students with same height are considered identical for ordering? Wait: It says "It is possible that some students possess similar heights." So there can be multiple students with same height. But when they have same height, they are indistinguishable? In terms of arrangement, the target a is a list of heights. The initial b is a list of heights. We need to rearrange the children (they are distinct) so that the heights match the target sequence. Since children with same height are considered identical only by height, but they are still distinct people; however, if two children have same height, swapping them doesn't change the height sequence. The problem asks for resulting order of heights to be a. So if multiple children have same height, any of them placed at a particular index where the target height is that value is acceptable. So the identity beyond height doesn't matter. Therefore, any mapping that pairs occurrences of same height works. So greedy with any j works.
But caution: If we choose j arbitrarily, could we later run into a situation where we need a value that is not available because we used the wrong occurrence? But since we always take an occurrence of the needed height from positions > i, and the counts of heights in the remaining part must match the counts in a[i+1..n] because of multiset equality and the fact we already matched prefix correctly, there will always be at least one occurrence of the needed height in the suffix. So it's fine.
Thus algorithm:
Initialize array cur = b.
List of moves: each move is (pos, pos+1) where we swap cur[pos] and cur[pos+1].
We'll generate moves as we simulate.
For i from 1 to n:
if cur[i] == a[i]: continue.
else:
find index j (i+1 <= j <= n) such that cur[j] == a[i].
for k from j-1 down to i:
swap cur[k] and cur[k+1]; record move (k, k+1) (or maybe print k? Need to print positions 1-indexed, but in example they print e.g., "2 3" meaning swap positions 2 and 3).
// After loop, cur[i] becomes a[i].
That's it.
Complexity O(n^2) operations and moves count <= n*(n-1)/2 = ~45k for n=300, which is far below 1e6.
But wait: The example output uses 4 moves for n=4: Input: a=1 2 3 2, b=3 2 1 2. Let's simulate:
cur=[3,2,1,2]
i=1: need a1=1. cur[1]=3, not 1. Find j with cur[j]=1 -> j=3.
Move j from 3 to 1:
swap pos2-3: (2,3) -> cur becomes [3,1,2,2]
swap pos1-2: (1,2) -> cur becomes [1,3,2,2]
Now i=1 fixed.
Record moves: 2 3, 1 2? Actually in our loop: for k from j-1=2 down to i=1: swap(k,k+1). So first swap(2,3), then swap(1,2). That yields moves: (2,3) then (1,2). In example output, moves: 2 3; 1 2; 3 4; 2 3. They have 4 moves, while we have only 2 after first step? Let's continue.
i=2: cur=[1,3,2,2]; need a2=2. cur[2]=3 !=2. Find j>2 with cur[j]=2: j=3 (cur[3]=2) or j=4. Let's pick j=3.
Move from 3 to 2: swap(2,3) -> cur becomes [1,2,3,2]. Record move (2,3). So moves: 2 3 (again). Now i=2 fixed.
i=3: cur=[1,2,3,2]; need a3=3. cur[3]=3 matches. Continue.
i=4: cur=[1,2,3,2]; need a4=2. cur[4]=2 matches.
So we got moves: (2,3), (1,2), (2,3). That's 3 moves, but example output has 4 moves: 2 3; 1 2; 3 4; 2 3. They have an extra swap (3 4). Why? Because they might have chosen j=4 at step i=2 instead of j=3? Let's test: i=2, need a2=2, cur=[1,3,2,2]. If we pick j=4: move from 4 to 2:
swap(3,4) -> cur=[1,3,2,2]? Wait careful: swapping positions 3 and 4: cur becomes [1,3,2,2]? Actually initial: [1,3,2,2]; swap(3,4) => positions 3 and 4: values 2 and 2, so still [1,3,2,2]. Then swap(2,3): now positions 2 (3) and 3 (2) => [1,2,3,2]. So moves: (3,4) then (2,3). So total moves: earlier (2,3),(1,2) from step1, plus (3,4),(2,3) = 4 moves. So both produce correct final array. Our algorithm can pick any j. The example output used j=4 at i=2. So our algorithm can produce either depending on which j we choose. It doesn't matter as long as we produce a valid sequence.
Thus greedy works.
Now we must ensure number of moves <= 1e6. For n=300, max moves about 45k, safe. But if n=300, then worst-case moves is sum_{i=1}^{n} (j-i) <= n*(n-1)/2 = 44,850. But we need to output moves. We must ensure we print in correct format.
Edge Cases: n=1, no moves.
Also need to guarantee that we can find j > i with cur[j]==a[i]. Since multisets match and prefix up to i-1 already matches a[1..i-1], the count of a[i] in cur[i..n] must be >= count of a[i] in a[i..n]? Actually we can prove that there will be at least one such j because after fixing prefix, the suffix must contain a[i]. Let's be safe: we can just search from i+1 to n; if not found, maybe it's at i itself? But we already checked cur[i]!=a[i]; but if it's not found beyond i, then there is no occurrence of a[i] in the suffix, which would mean the counts don't match, contradicting guarantee. So it's fine.
Now we need to implement. We'll store cur as list of ints (0-indexed). For each i (0-indexed loop for i in range(n)):
if cur[i] != a[i]:
# find index j from i+1 to n-1 where cur[j] == a[i]
for j in range(i+1, n):
if cur[j] == a[i]:
break
# now we will move from j down to i+1 by swapping adjacent leftwards
for k in range(j-1, i-1, -1):
# swap cur[k], cur[k+1]
cur[k], cur[k+1] = cur[k+1], cur[k]
moves.append((k+1, k+2)) # since positions are 1-indexed in output
This yields O(n^2) swaps.
But wait: When we swap, we should record the positions of the swap (the two adjacent indices). In output they list "pi pi+1" meaning we swap positions pi and pi+1. So if we swap indices k and k+1 (0-indexed), then pi = k+1, pi+1 = k+2.
Now we need to ensure that k (0-indexed) is from j-1 down to i inclusive? Let's derive: We have element at j that we want to bring to i. After each swap with its left neighbor, it moves left by one. We need to swap (j-1,j), then (j-2,j-1), ..., (i,i+1). That's j-i swaps. So k runs from j-1 down to i. For each swap at indices (k,k+1). So in loop: for k in range(j-1, i-1, -1): do swap at k and k+1. Good.
Now we need to check if there is any nuance: Because after we perform swaps, the array changes, but we are still within the loop for i. That's fine.
Now about possible improvement: None needed.
Now confirm with example: n=4, a=[1,2,3,2], b=[3,2,1,2].
i=0: cur[0]=3 !=1. Find j: from 1 to 3, cur[1]=2, cur[2]=1 -> j=2 (0-indexed). Then for k from 1 down to 0: first k=1: swap indices1 and2: cur becomes [3,1,2,2]; record move (2,3). Then k=0: swap indices0 and1: cur becomes [1,3,2,2]; record (1,2). So moves: (2,3), (1,2). i=1: cur[1]=3 !=2. Find j from 2: cur[2]=2 -> j=2 (0-indexed). For k from 1 down to 1: k=1: swap indices1 and2: cur becomes [1,2,3,2]; record (2,3). So moves: (2,3) again. That's 3 moves. But the example shows 4 moves. That's fine. Our output would be:
3
2 3
1 2
2 3
But is it accepted? Yes, because they don't require minimal moves. So it's acceptable.
But we must ensure that the number of moves does not exceed 1e6. Our method will produce at most 44,850 moves, so safe.
Thus we can output.
Now we need to consider if n can be as high as 300, and values up to 1e9, but no issue.
Edge case: duplicates may cause that when we search for j, we might find an index but after moving it, the relative ordering among duplicates might change but that's okay.
Now let's test with a more complex scenario: a = [2,1,2], b = [2,2,1]. i=0: cur[0]=2 matches a[0]=2, skip. i=1: cur[1]=2 != a[1]=1. Find j>1: cur[2]=1, j=2. Swap indices1 and2: cur becomes [2,1,2]; record (2,3). i=2: cur[2]=2 matches. Works.
If we had a = [1,1,2], b=[2,1,1]. i=0: need 1, cur[0]=2 !=1. Find j=1 (cur[1]=1) or j=2 (cur[2]=1). Choose j=1: swap (1,2) => cur becomes [1,2,1]; record (1,2). i=1: cur[1]=2 !=1. Find j=2 (cur[2]=1): swap (2,3) => cur becomes [1,1,2]; record (2,3). Works.
If we choose j=2 at first: i=0: need 1, j=2. Swap (2,3): cur becomes [2,1,1]; then swap (1,2): cur becomes [1,2,1]; record (2,3),(1,2). i=1: then need 1, j=2: swap (2,3) => [1,1,2]; record (2,3). So moves count 3. Both fine.
Now implement.
But wait: There's a subtlety: The initial array b and target a are given as heights. However, the problem says "it is possible to arrange the children in the required order, i.e. a and b coincide as multisets." So we are guaranteed that the multiset of heights is same. But there may be multiple children with same height; we treat them as indistinguishable. So the approach works.
Now code:
Read n.
Read list a (n ints)
Read list b (n ints)
cur = b[:]
moves = [] # list of (p, p+1)
for i in range(n):
if cur[i] != a[i]:
# find j > i with cur[j] == a[i]
j = i+1
while cur[j] != a[i]:
j += 1
# Now move left
for k in range(j-1, i-1, -1):
cur[k], cur[k+1] = cur[k+1], cur[k]
moves.append((k+1, k+2)) # 1-indexed positions
Output len(moves) and then each move
print(len(moves))
for p1, p2 in moves:
print(p1, p2)
Check constraints: number of moves could be 0, then output only 0? Yes, first line 0 and then nothing.
But we need to ensure we don't exceed 1e6 moves; our method is safe.
Double-check maximum moves: worst case when b is reverse of a (all distinct). Then for i from 1 to n, distance j-i could be as large as n-i. Sum_{i=1}^n (n-i) = n(n-1)/2. For n=300, that's 44850. So fine.
Now for n=300, O(n^2) ~ 90k operations, which is trivial.
Thus solution is straightforward.
But is there any scenario where this greedy fails because we need to preserve order of duplicates? Suppose a = [1,2,1], b = [1,1,2]. The greedy: i=1 (0-indexed i=1): need a[1]=2, cur[1]=1 !=2. Find j=2 (cur[2]=2). Swap (2,3) => cur becomes [1,2,1]; moves recorded. Works.
If we had a = [2,2,1], b = [2,1,2]. i=1: need a[1]=2, cur[1]=1 !=2. Find j=2 (cur[2]=2). Swap (2,3) => [2,2,1]; works.
If a = [2,1,2], b = [1,2,2]. i=0: need 2, cur[0]=1 !=2. Find j=1? cur[1]=2 => j=1. Swap (1,2) => [2,1,2]; works.
Seems fine.
Thus implement.
But wait, some might worry about the while loop for j; we can just do a linear search; n is small.
Now, there is a nuance: The problem statement says: "It is not required to minimize the number of moves but it must not exceed 10^6." So we must ensure that even in worst case our algorithm does not exceed 1e6. For n=300, it's far below. However, if n were larger (but limit is 300), it's still safe.
Now let's consider if there's any scenario where j-i could be > n-i? Actually j is found > i, so at most n-1 - i + 1? Actually max j is n-1 (0-indexed), so j-i <= n-1-i = n-1-i. So sum < n(n-1)/2.
Thus solution is correct.
We should also consider that the swap sequence we output must be such that applying it to the initial b yields a. Our simulation does exactly that. So final cur becomes a. We could even verify after generating.
Now, there is another potential approach: simulate bubble sort of b to reorder to a using a stable mapping. But our greedy is simpler.
One more nuance: Since we are allowed up to 1e6 moves, we might also consider using an algorithm that sorts by moving each element from left to right; it's the same.
Thus final answer: implement as described.
Now produce final output.
But I'm ChatGPT, I need to output the solution code in the final answer? The user asked "Help Vasya, find the sequence of swaps..." So they likely expect a code solution. But as ChatGPT, I need to provide an explanation and maybe the code? The problem is from a competitive programming context; the user is likely asking for solution. So I'll provide a solution in Python.
I should also consider possible pitfalls: Input reading, large numbers, but no issue.
Now I'll produce the final answer with explanation and code.
But the instruction says: "You are an AI assistant." Not explicit about output format. Usually we output solution in a coding style. I'll include explanation and then the code.
Better to output the solution as described.
Now let me write the solution in Python:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
a = [int(next(it)) for _ in range(n)]
b = [int(next(it)) for _ in range(n)]
cur = b[:]
moves = []
for i in range(n):
if cur[i] == a[i]:
continue
# find index j > i with cur[j] == a[i]
j = i+1
while j < n and cur[j] != a[i]:
j += 1
# j must exist
for k in range(j-1, i-1, -1):
cur[k], cur[k+1] = cur[k+1], cur[k]
moves.append((k+1, k+2))
# output
print(len(moves))
for x,y in moves:
print(x, y)
if name == "main":
solve()
Now test with example:
Input:
4
1 2 3 2
3 2 1 2
Our code will produce:
i=0: cur[0]=3!=1, find j=2 (since cur[2]=1). Then swaps: k=1 => swap (2,3) => cur [3,1,2,2]; k=0 => swap (1,2) => [1,3,2,2]; moves: (2,3),(1,2)
i=1: cur[1]=3!=2, find j=2? cur[2]=2, so j=2; then k=1 => swap (2,3) => [1,2,3,2]; moves: (2,3)
i=2,3 match. So output:
3
2 3
1 2
2 3
But example output has 4 moves. But that's fine. Should be accepted.
But is there any requirement about formatting? They printed moves lines with two integers separated by space. So it's fine.
Now test second example: n=2, a=[1,100500], b=[1,100500] => no moves, output 0.
Thus okay.
Now ensure that we didn't exceed 1e6. For n=300, max moves ~45k.
Now note: Since the problem says "It is not required to minimize the number of moves but it must not exceed 10^6." In worst-case n=300, we are safe. But if n were larger, say n=1000, then moves ~500k, still <1e6. But n max is 300, so safe.
Thus final answer.
But I'm not completely comfortable: There is one nuance: when we find j, we might be moving an element over others that have the same value. That's fine.
Now, I'll add a bit more explanation: We can treat the process as reconstructing the target by repeatedly bringing the required element to the front.
Now provide final answer.
One more thing: The problem says "Vasya can swap two people who stand next to each other in the line." So our moves are adjacent swaps.
All good.
Thus final answer with code.
But maybe the problem expects to output pi and pi+1 on each line, meaning the positions to swap. In our output we print two numbers: the first is the left index, the second is the right index (which is first+1). That's correct.
Now, I'll produce final answer. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
n = int(next(it))
a = [int(next(it)) for _ in range(n)]
b = [int(next(it)) for _ in range(n)]
cur = b[:] # current line
moves = [] # list of (left_pos, right_pos), 1-indexed
for i in range(n):
if cur[i] == a[i]:
continue
# find the first occurrence of a[i] to the right of i
j = i + 1
while j < n and cur[j] != a[i]:
j += 1
# bring it to position i by adjacent swaps
for k in range(j - 1, i - 1, -1):
cur[k], cur[k + 1] = cur[k + 1], cur[k]
moves.append((k + 1, k + 2)) # convert to 1-indexed
# output
print(len(moves))
for x, y in moves:
print(x, y)
if name == "main":
solve()