#P15576. [USACO26FEB] Good Cyclic Shifts G

    ID: 15483 远端评测题 2000ms 256MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>线段树USACO树状数组前缀和差分2026

[USACO26FEB] Good Cyclic Shifts G

题目描述

For a permutation p1,p2,,pNp_1,p_2,\dots,p_N of 1N1\dots N (1N21051\le N\le 2\cdot 10^5), let f(p)=i=1Npii2f(p)=\sum_{i=1}^N \frac{|p_i-i|}{2}. A permutation is good if it can be turned into the identity permutation using at most f(p)f(p) swaps of adjacent elements.

Given a permutation, find which cyclic shifts of it are good.

输入格式

The input consists of TT (1T1051\le T\le 10^5) independent tests. Each test is specified as follows:

The first line contains NN.

The second line contains p1,p2,,pNp_1,p_2,\dots,p_N (1piN1\le p_i\le N), which is guaranteed to be a permutation of 1N1\dots N.

It is guaranteed that the sum of NN over all tests does not exceed 10610^6.

输出格式

For each test, output two lines:

On the first line, output the number of good cyclic shifts kk.

Then output a line with kk space-separated integers ss (0s<N0\le s<N) in increasing order, meaning that pp is good when cyclically shifted to the right ss 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 p=[1,2,4,3]p = [1, 2, 4, 3].

  • $f(p) = (|1 - 1| + |2 - 2| + |4 - 3| + |3 - 4|) / 2 = 1$. Since pp can be turned into the identity permutation in one move by swapping p3p_3 and p4p_4, pp is good.
  • Cyclically shifting pp to the right 11 time, we get q=[3,1,2,4]q = [3, 1, 2, 4]. $f(q) = (|3 - 1| + |1 - 2| + |2 - 3| + |4 - 4|) / 2 = 2$. Since qq can be turned into the identity permutation in two moves by swapping q1q_1 two steps forward, qq is good. It can be seen that the other two cyclic shifts are not good.

SCORING:

  • Input 2: N10N\le 10
  • Inputs 3-5: T10,N2000T\le 10, N\le 2000
  • Inputs 6-11: No additional constraints.

Problem credits: Akshaj Arora