#5445. Problem 1. Good Cyclic Shifts
Problem 1. Good Cyclic Shifts
Problem 1. Good Cyclic Shifts
USACO 2026 Third Contest, Gold
For a permutation of (), let . A permutation is
good
if it can be turned into the identity permutation using at most swaps of adjacent elements.
Given a permutation, find which cyclic shifts of it are good.
INPUT FORMAT (input arrives from the terminal / stdin):
The input consists of () independent tests. Each test is specified as follows:
The first line contains .
The second line contains (), which is guaranteed to be a permutation of .
It is guaranteed that the sum of over all tests does not exceed .
OUTPUT FORMAT (print output to the terminal / stdout):
For each test, output two lines:
On the first line, output the number of good cyclic shifts .
Then output a line with space-separated integers () in increasing order, meaning that is good when cyclically shifted to the right times.
SAMPLE INPUT:
3 5 5 4 3 2 1 4 1 2 4 3 5 1 2 3 4 5
SAMPLE OUTPUT:
0 2 0 1 5 0 1 2 3 4
Consider the second test case, where .
- $f(p) = (|1 - 1| + |2 - 2| + |4 - 3| + |3 - 4|) / 2 = 1$. Since can be turned into the identity permutation in one move by swapping and , is good.
- Cyclically shifting to the right time, we get . $f(q) = (|3 - 1| + |1 - 2| + |2 - 3| + |4 - 4|) / 2 = 2$. Since can be turned into the identity permutation in two moves by swapping two steps forward, is good.
It can be seen that the other two cyclic shifts are not good.
SCORING: Input 2: Inputs 3-5: Inputs 6-11: No additional constraints.
Problem credits: Akshaj Arora