题目 › usaco-1593

Good Cyclic Shifts

USACO 2026 Third · 金组 · 2026-02 · L3 困难 · C++17 · 时限 2 秒 · 测试点等分

查看原题跳到结果

题面

来源:USACO 官网。模型收到的是同一份题面的文字。

For a permutation $p_1,p_2,\dots,p_N$ of $1\dots N$ ($1\le N\le 2\cdot 10^5$), let $f(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)$ 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 $T$ ($1\le T\le 10^5$) independent tests. Each test is specified as follows:

The first line contains $N$.

The second line contains $p_1,p_2,\dots,p_N$ ($1\le p_i\le N$), which is guaranteed to be a permutation of $1\dots N$.

It is guaranteed that the sum of $N$ over all tests does not exceed $10^6$.

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 $k$.

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

  • $f(p) = (|1 - 1| + |2 - 2| + |4 - 3| + |3 - 4|) / 2 = 1$. Since $p$ can be turned into the identity permutation in one move by swapping $p_3$ and $p_4$, $p$ is good.
  • Cyclically shifting $p$ to the right $1$ time, we get $q = [3, 1, 2, 4]$. $f(q) = (|3 - 1| + |1 - 2| + |2 - 3| + |4 - 4|) / 2 = 2$. Since $q$ can be turned into the identity permutation in two moves by swapping $q_1$ two steps forward, $q$ is good.

It can be seen that the other two cyclic shifts are not good.

SCORING:

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

Problem credits: Akshaj Arora

结果

Kimi K3.1 ProKimi

auto(max)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 7.9k平均输出 token(含思考) ↓
  • 27.2输出速度 tokens/s ↑
  • 273 秒延迟 ↓
  • 0.74×CPU 时间 / 官方解 ↓
  • 同阶 3时间复杂度(相对官方解)

DeepSeek Flash V4.1-1017DeepSeek

auto(high)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 14.2k平均输出 token(含思考) ↓
  • 225.6输出速度 tokens/s ↑
  • 65 秒延迟 ↓
  • 0.70×CPU 时间 / 官方解 ↓
  • 同阶 3时间复杂度(相对官方解)

MiniMax M3.1 FlashMiniMax

auto(max)

3/3

解出的采样 ↑

  • 100采样 1 · 解出
  • 100采样 2 · 解出
  • 100采样 3 · 解出
  • 24.5k平均输出 token(含思考) ↓
  • 165.0输出速度 tokens/s ↑
  • 147 秒延迟 ↓
  • 0.69×CPU 时间 / 官方解 ↓
  • 同阶 3时间复杂度(相对官方解)

↑ 越大越好,↓ 越小越好。得分 0–100,按官方时限判定,所有测试点都满分才算解出。CPU 时间 / 官方解为解出的答案在官方解最慢的 3 个测试点上的 CPU 时间之比(几何平均),只比较官方解至少需要 0.05 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。

代码

Kimi K3.1 Pro · 采样 1 · 100 行
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int T;
    cin >> T;

    while (T--) {
        int N;
        cin >> N;

        vector<int> p(N);
        for (int &x : p) cin >> x;

        // Initial inversion count.
        vector<int> bit(N + 1, 0);
        auto add = [&](int i, int v) {
            for (; i <= N; i += i & -i) bit[i] += v;
        };
        auto sum = [&](int i) {
            int r = 0;
            for (; i > 0; i -= i & -i) r += bit[i];
            return r;
        };

        long long inv = 0;
        for (int i = 0; i < N; ++i) {
            inv += i - sum(p[i]);
            add(p[i], 1);
        }

        long long f = 0;
        for (int i = 0; i < N; ++i) {
            f += abs((long long)p[i] - (i + 1));
        }
        f /= 2;

        /*
         * For the current permutation q, let d_i = q_i - i and
         * L = #{i : d_i <= 0}.
         *
         * After a right cyclic shift whose last element is x:
         *   inv' = inv + 2*x - N - 1
         *   f'   = f + L + x - N - 1
         *
         * Keys represent d_i with a global offset: at shift s,
         * d_i = key_i - s. Each ordinary position change subtracts 1
         * from d_i; the wrapped element additionally has its key
         * increased by N.
         */
        const int offset = N + 1;
        vector<int> key(N), freq(3 * N + 5, 0);
        int L = 0;

        for (int i = 0; i < N; ++i) {
            key[i] = p[i] - (i + 1);
            ++freq[key[i] + offset];
            L += (key[i] <= 0);
        }

        vector<int> answer;
        answer.reserve(N);

        for (int s = 0; s < N; ++s) {
            if (inv <= f) answer.push_back(s);
            if (s + 1 == N) break;

            int idx = N - 1 - s;
            int x = p[idx];

            f += L + x - N - 1;
            inv += 2LL * x - N - 1;

            int threshold = s + 1;
            L += freq[threshold + offset];

            // Remove the virtual value of the wrapped element and insert
            // its actual new value.
            --L;
            if (x == 1) ++L;

            int oldKey = key[idx];
            --freq[oldKey + offset];
            key[idx] += N;
            ++freq[key[idx] + offset];
        }

        cout << answer.size() << '\n';
        for (int i = 0; i < (int)answer.size(); ++i) {
            if (i) cout << ' ';
            cout << answer[i];
        }
        cout << '\n';
    }

    return 0;
}