Project 15 Puzzle · Solving Any Configuration

Lesson 4

Nikolai Chukhin · Alexander S. Kulikov

Programming problem. Given two permutations of \(\{0, \dotsc, n\}\), transform the first permutation into the second one using at most \(n\) \(3\)-cycles.

  • Input format.  The first line contains the integer \(n\) (\(3 \le n \le 1000\)). The next two lines contain \(n+1\) integers separated by spaces — the elements of the permutations. It is guaranteed that each line defines a valid permutation, that is, \(\pi[i] \ne \pi[j]\) for \(i \ne j\) and \(0 \le \pi[i] \le n\).

  • Output format.  The first line of the output should contain one integer \(t\) such that \(0 \le t \le n\) — the number of \(3\)-cycles used. Each of the next \(t\) lines should contain the descriptions of \(3\)-cycles as three different indices \(0 \le i < j < k \le n\) separated by spaces. If it is impossible to transform the first permutation into the second one using \(3\)-cycles, print \(-1\).

5 points
Public samples
Public sample 1
Input
3
3 0 2 1
0 1 2 3
Expected output
2
0 1 3
0 1 3