Two Permutations (Easy Version) (Codeforces 1882E1)
Tags: Greedy, number theory

This is the easy version of the problem. The difference between the two versions is that you do not have to minimize the number of operations in this version. You can make hacks only if both versions of the problem are solved.You have two permutations$$$^{\dagger}$$$ $$$p_{1}, p_{2}, \ldots, p_{n}$$$ (of integers $$$1$$$ to $$$n$$$) and $$$q_{1}, q_{2}, \ldots, q_{m}$$$ (of integers $$$1$$$ to $$$m$$$). Initially $$$p_{i}=a_{i}$$$ for $$$i=1, 2, \ldots, n$$$, and $$$q_{j} = b_{j}$$$ for $$$j = 1, 2, \ldots, m$$$. You can apply the following operation on the permutations several (possibly, zero) times.In one operation, $$$p$$$ and $$$q$$$ will change according to the following three steps:  You choose integers $$$i$$$, $$$j$$$ which satisfy $$$1 \le i \le n$$$ and $$$1 \le j \le m$$$.  Permutation $$$p$$$ is partitioned into three parts using $$$p_i$$$ as a pivot: the left part is formed by elements $$$p_1, p_2, \ldots, p_{i-1}$$$ (this part may be empty), the middle part is the single element $$$p_i$$$, and the right part is $$$p_{i+1}, p_{i+2}, \ldots, p_n$$$ (this part may be empty). To proceed, swap the left and the right parts of this partition. Formally, after this step, $$$p$$$ will become $$$p_{i+1}, p_{i+2}, \ldots, p_{n}, p_{i}, p_{1}, p_{2}, \ldots, p_{i-1}$$$. The elements of the newly formed $$$p$$$ will be reindexed starting from $$$1$$$.  Perform the same transformation on $$$q$$$ with index $$$j$$$. Formally, after this step, $$$q$$$ will become $$$q_{j+1}, q_{j+2}, \ldots, q_{m}, q_{j}, q_{1}, q_{2}, \ldots, q_{j-1}$$$. The elements of the newly formed $$$q$$$ will be reindexed starting from $$$1$$$. Your goal is to simultaneously make $$$p_{i}=i$$$ for $$$i=1, 2, \ldots, n$$$, and $$$q_{j} = j$$$ for $$$j = 1, 2, \ldots, m$$$.Find any valid way to achieve the goal using at most $$$10\,000$$$ operations, or say that none exists. Please note that you do not have to minimize the number of operations.It can be proved that if it is possible to achieve the goal, then there exists a way to do so using at most $$$10\,000$$$ operations.$$$^{\dagger}$$$ A permutation of length $$$k$$$ is an array consisting of $$$k$$$ distinct integers from $$$1$$$ to $$$k$$$ in arbitrary order. For example, $$$[2,3,1,5,4]$$$ is a permutation, but $$$[1,2,2]$$$ is not a permutation ($$$2$$$ appears twice in the array), and $$$[1,3,4]$$$ is also not a permutation ($$$k=3$$$ but there is $$$4$$$ in the array).

Input: The first line contains two integers $$$n$$$ and $$$m$$$ ($$$1 \le n, m \le 2500$$$).The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \le a_i \le n$$$).The third line contains $$$m$$$ integers $$$b_1, b_2, \ldots, b_m$$$ ($$$1 \le b_i \le m$$$).It is guaranteed that $$$a$$$ and $$$b$$$ are permutations.

Output: If there is no solution, print a single integer $$$-1$$$.Otherwise, print an integer $$$k$$$ ($$$0 \le k \le 10\,000$$$) — the number of operations to perform, followed by $$$k$$$ lines, each containing two integers $$$i$$$ and $$$j$$$ ($$$1 \le i \le n$$$, $$$1 \le j \le m$$$) — the integers chosen for the operation.If there are multiple solutions, print any of them.Please note that you do not have to minimize the number of operations.

Note: In the first example, we can achieve the goal within $$$2$$$ operations:   In the first operation, choose $$$i = 3$$$, $$$j = 4$$$. After this, $$$p$$$ becomes $$$[3, 2, 1]$$$ and $$$q$$$ becomes $$$[3, 4, 5, 2, 1]$$$.  In the second operation, choose $$$i = 2$$$, $$$j = 4$$$. After this, $$$p$$$ becomes $$$[1, 2, 3]$$$ and $$$q$$$ becomes $$$[1, 2, 3, 4, 5]$$$. In the third example, it is impossible to achieve the goal.

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

4 4
3 4 2 1
2 4 1 3

2 2
1 2
2 1

Sample Outputs:
2
3 4
2 4

5
4 2
3 3
1 4
3 2
4 1

-1

Solution (Python):
def read_int():
    return int(input())

def read_list():
    return list(map(int, input().split()))

def main():
    solve()

def solve():
    (n, m) = read_list()
    p = read_list()
    p = [x - 1 for x in p]
    q = read_list()
    q = [x - 1 for x in q]
    a = [-1 for _ in range(n)]
    for (i, x) in enumerate(p):
        a[x] = i
    b = [-1 for _ in range(m)]
    for (i, x) in enumerate(q):
        b[x] = i

    def operator(p, a, n):
        ans = []
        for i in range(n):
            if p[i] != i:
                j = a[i]
                ans.append(i)
                ans.append(j - i - 1)
                ans.append(n - j - 1)
                (p[i], p[j]) = (p[j], p[i])
                (a[i], a[p[j]]) = (i, j)
        return ans
    ans_p = operator(p, a, n)
    ans_q = operator(q, b, m)
    if len(ans_p) == len(ans_q):
        print(len(ans_p))
        for (x, y) in zip(ans_p, ans_q):
            print(x + 1, y + 1)
        return
    k = abs(len(ans_q) - len(ans_p))
    if k % 2 == 1 and n % 2 == 0 and (m % 2 == 0):
        print(-1)
        return
    if k % 2 == 1:
        if n % 2 == 1:
            for _ in range(n):
                ans_p.append(0)
        else:
            for _ in range(m):
                ans_q.append(0)
    k = abs(len(ans_q) - len(ans_p))
    if len(ans_p) < len(ans_q):
        for _ in range(k // 2):
            ans_p.append(0)
            ans_p.append(n - 1)
    else:
        for _ in range(k // 2):
            ans_q.append(0)
            ans_q.append(m - 1)
    print(len(ans_p))
    for (x, y) in zip(ans_p, ans_q):
        print(x + 1, y + 1)
if __name__ == '__main__':
    main()

Solution (Python):
import sys
R = sys.stdin.readline
S = lambda : map(int, R().split())
(n, m) = S()
(a, b) = ([*S()], [*S()])
g = lambda j, a: a[j + 1:] + [a[j]] + a[0:j]

def f(k, a):
    r = []
    for i in range(k, 0, -1):
        j = a.index(i)
        if j + 1 == i:
            continue
        if i == k:
            r += (j + 2,)
            a = g(j + 1, a)
            continue
        r += (j + 1,)
        a = g(j, a)
        r += (i - j - 1,)
        a = g(i - j - 2, a)
        r += (k - i + 1,)
        a = g(k - i, a)
    return r
(r1, r2) = (f(n, a), f(m, b))
if not n & 1 and (not m & 1) and len(r1) + len(r2) & 1:
    print(-1)
    exit()
if len(r1) + len(r2) & 1:
    if n & 1:
        r1 += [1] * n
    else:
        r2 += [1] * m
g = len(r1) - len(r2)
if g > 0:
    r2 += [1, m] * (g // 2)
elif g < 0:
    r1 += [1, n] * (-g // 2)
print(len(r1))
for (c, d) in zip(r1, r2):
    print(c, d)

Solution (Python):
import sys
R = sys.stdin.readline
S = lambda : map(int, R().split())
(n, m) = S()
(a, b) = ([*S()], [*S()])
g = lambda j, a: a[j + 1:] + [a[j]] + a[0:j]

def f(k, a):
    r = []
    for i in range(k, 0, -1):
        j = a.index(i)
        if j + 1 == i:
            continue
        if i == k:
            r += (j + 2,)
            a = g(j + 1, a)
            continue
        r += (j + 1,)
        a = g(j, a)
        r += (i - j - 1,)
        a = g(i - j - 2, a)
        r += (k - i + 1,)
        a = g(k - i, a)
    return r
(r1, r2) = (f(n, a), f(m, b))
if not n & 1 and (not m & 1) and len(r1) + len(r2) & 1:
    print(-1)
    exit()
if len(r1) + len(r2) & 1:
    if n & 1:
        r1 += [1] * n
    else:
        r2 += [1] * m
g = len(r1) - len(r2)
if g > 0:
    r2 += [1, m] * (g // 2)
elif g < 0:
    r1 += [1, n] * (-g // 2)
print(len(r1))
for (c, d) in zip(r1, r2):
    print(c, d)