#P15576. [USACO26FEB] Good Cyclic Shifts G
[USACO26FEB] Good Cyclic Shifts G
题目描述
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.
输入格式
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 .
输出格式
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.
3
5
5 4 3 2 1
4
1 2 4 3
5
1 2 3 4 5
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