Physical Education (Codeforces 53D)
Tags: Others, sortings

Vasya is a school PE teacher. Unlike other PE teachers, Vasya doesn't like it when the students stand in line according to their height. Instead, he demands that the children stand in the following order: a1, a2, ..., an, where ai is the height of the i-th student in the line and n is the number of students in the line. The children find it hard to keep in mind this strange arrangement, and today they formed the line in the following order: b1, b2, ..., bn, which upset Vasya immensely. Now Vasya wants to rearrange the children so that the resulting order is like this: a1, a2, ..., an. During each move Vasya can swap two people who stand next to each other in the line. Help Vasya, find the sequence of swaps leading to the arrangement Vasya needs. It is not required to minimize the number of moves.

Input: The first line contains an integer n (1 ≤ n ≤ 300) which is the number of students. The second line contains n space-separated integers ai (1 ≤ ai ≤ 109) which represent the height of the student occupying the i-th place must possess. The third line contains n space-separated integers bi (1 ≤ bi ≤ 109) which represent the height of the student occupying the i-th place in the initial arrangement. It is possible that some students possess similar heights. It is guaranteed that it is possible to arrange the children in the required order, i.e. a and b coincide as multisets.

Output: In the first line print an integer k (0 ≤ k ≤ 106) which is the number of moves. It is not required to minimize k but it must not exceed 106. Then print k lines each containing two space-separated integers. Line pi, pi + 1 (1 ≤ pi ≤ n - 1) means that Vasya should swap students occupying places pi and pi + 1.


Sample Inputs:
4
1 2 3 2
3 2 1 2

2
1 100500
1 100500

Sample Outputs:
4
2 3
1 2
3 4
2 3

0

Solution (Python):
def vasya_and_physcult(count, a_str, b_str):
    size = int(count)
    a = list(map(int, a_str.split()))
    b = list(map(int, b_str.split()))
    changes_count = 0
    result = ''
    for i in range(size):
        current_index = i
        for j in range(i, size):
            if b[j] == a[i]:
                current_index = j
                break
        while current_index > i:
            (b[current_index], b[current_index - 1]) = (b[current_index - 1], b[current_index - 1])
            result += '\n' + f'{current_index} {current_index + 1}'
            changes_count += 1
            current_index -= 1
    return str(changes_count) + result
count = input()
a = input()
b = input()
print(vasya_and_physcult(count, a, b))

Solution (Python):
import math
import random
from datetime import datetime
now = datetime.now()

def prime_generator(nr_elemente_prime):
    vector_prime = [-1] * nr_elemente_prime
    vector_rasp = [0] * nr_elemente_prime
    vector_prime[1] = 1
    vector_rasp[1] = 1
    contor = 2
    for i in range(2, nr_elemente_prime):
        if vector_prime[i] == -1:
            vector_prime[i] = 1
            vector_rasp[contor] = i
            contor = contor + 1
            for j in range(i + i, nr_elemente_prime, i):
                if vector_prime[j] == -1:
                    vector_prime[j] = i
    set_prime = set(vector_rasp[2:len(vector_rasp)])
    set_prime.remove(0)
    return list(set_prime)
rasp_final = ''
dictionar = {}
dictionar[0] = 0
dictionar[1] = 1
palindrom = []
palindrom.append(0)
palindrom.append(1)
contor = 2
for tt in range(1):
    n = int(input())
    bloc = list(map(int, input().split()))
    lista = list(map(int, input().split()))
    cate = 0
    answ = ''
    for i in range(n):
        if bloc[i] != lista[i]:
            for j in range(i + 1, n):
                if lista[j] == bloc[i]:
                    for z in range(j - 1, i - 1, -1):
                        cate += 1
                        answ += str(z + 1) + ' ' + str(z + 2) + '\n'
                        c = lista[z + 1]
                        lista[z + 1] = lista[z]
                        lista[z] = c
                    break
    print(cate)
    print(answ)

Solution (Python):
i = int(input())
b = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = []
for (i, x) in enumerate(b):
    c = a.index(x)
    for y in range(c):
        ans.append([c + i - y, c + i - y + 1])
    a.remove(x)
print(len(ans))
for x in ans:
    print(*x)