题面
来源: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
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
3/3
解出的采样 ↑
- 100采样 1 · 解出
- 100采样 2 · 解出
- 100采样 3 · 解出
- 14.2k平均输出 token(含思考) ↓
- 225.6输出速度 tokens/s ↑
- 65 秒延迟 ↓
- 0.70×CPU 时间 / 官方解 ↓
- 同阶 3时间复杂度(相对官方解)
MiniMax M3.1 FlashMiniMax
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 秒的题;复杂度按增长幂次相对官方解分类。判题与计分见 原理。
代码
#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; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; struct Fenwick { int n; vector<int> bit; explicit Fenwick(int n_) : n(n_), bit(n_ + 1, 0) {} void add(int index, int value) { for (; index <= n; index += index & -index) { bit[index] += value; } } int sum(int index) const { int result = 0; for (; index > 0; index -= index & -index) { result += bit[index]; } return result; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<int> p(N); vector<int> originalPosition(N + 1); for (int i = 0; i < N; ++i) { cin >> p[i]; originalPosition[p[i]] = i + 1; } Fenwick fenwick(N); int64 inversions = 0; for (int i = N - 1; i >= 0; --i) { inversions += fenwick.sum(p[i] - 1); fenwick.add(p[i], 1); } int64 totalDistance = 0; int positiveCount = 0; for (int i = 0; i < N; ++i) { totalDistance += abs(p[i] - (i + 1)); positiveCount += (p[i] > i + 1); } int64 f = totalDistance / 2; // diagonal[s] is the number of positions i with q_i = i + 1 // in the cyclic shift s. vector<int> diagonal(N, 0); for (int value = 2; value <= N; ++value) { int s = value - originalPosition[value] - 1; if (s < 0) { s += N; } ++diagonal[s]; } vector<int> answer; answer.reserve(N); for (int s = 0; s < N; ++s) { if (inversions <= f) { answer.push_back(s); } if (s + 1 == N) { break; } int x = p[N - 1 - s]; inversions += 2LL * x - N - 1; f += x - positiveCount - 1; positiveCount += (x > 1 ? 1 : 0) - diagonal[s]; } cout << answer.size() << '\n'; for (size_t i = 0; i < answer.size(); ++i) { if (i > 0) { cout << ' '; } cout << answer[i]; } cout << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using int64 = long long; struct Fenwick { int n; vector<int> bit; explicit Fenwick(int n) : n(n), bit(n + 1, 0) {} void add(int i, int value) { for (; i <= n; i += i & -i) bit[i] += value; } int sum(int i) const { int result = 0; for (; i > 0; i -= i & -i) result += bit[i]; return result; } }; 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; Fenwick fw(N); int64 inversions = 0; int64 displacement = 0; for (int i = 0; i < N; ++i) { inversions += i - fw.sum(p[i]); fw.add(p[i], 1); displacement += abs(p[i] - (i + 1)); } displacement /= 2; // positive[s] = number of indices i with q_i > i, // where q is the right cyclic shift by s. vector<int> diff(N + 1, 0); for (int j = 0; j < N; ++j) { int length = p[j] - 1; if (length == 0) continue; int start = (N - j) % N; if (start + length <= N) { ++diff[start]; --diff[start + length]; } else { ++diff[start]; --diff[N]; ++diff[0]; --diff[start + length - N]; } } vector<int> positive(N); int current = 0; for (int s = 0; s < N; ++s) { current += diff[s]; positive[s] = current; } vector<int> answer; for (int s = 0; s < N; ++s) { if (inversions <= displacement) answer.push_back(s); if (s + 1 == N) break; int x = p[N - 1 - s]; inversions += 2LL * x - N - 1; displacement += x - 1 - positive[s]; } cout << answer.size() << '\n'; for (int i = 0; i < (int)answer.size(); ++i) { if (i) cout << ' '; cout << answer[i]; } cout << '\n'; } return 0; }
#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 i = 0; i < N; ++i) { cin >> p[i]; --p[i]; } // Compute inversion count of the original permutation. long long inv = 0; vector<int> bit(N + 2, 0); for (int i = 0; i < N; ++i) { int v = p[i]; int sum = 0; for (int k = v + 1; k > 0; k -= k & -k) { sum += bit[k]; } inv += (long long)i - sum; for (int k = v + 1; k <= N; k += k & -k) { ++bit[k]; } } // We need F(s) = sum |p_j - ((j + s) mod N)| for all shifts s. // Add all V-shaped functions using range additions of linear functions. vector<long long> diffA(N + 2, 0), diffB(N + 2, 0); auto rangeAdd = [&](int l, int r, long long a, long long b) { if (l > r) return; diffA[l] += a; diffA[r + 1] -= a; diffB[l] += b; diffB[r + 1] -= b; }; auto addAbs = [&](int L, int R, int c) { if (L > R) return; // Add c - s on [L, min(R, c)] int r1 = min(R, c); if (L <= r1) { rangeAdd(L, r1, -1, c); } // Add s - c on [max(L, c + 1), R] int l2 = max(L, c + 1); if (l2 <= R) { rangeAdd(l2, R, 1, -c); } }; for (int j = 0; j < N; ++j) { int v = p[j]; // Before wrapping: s in [0, N - 1 - j] addAbs(0, N - 1 - j, v - j); // After wrapping: s in [N - j, N - 1] addAbs(N - j, N - 1, v - j + N); } vector<int> answer; long long coefA = 0, coefB = 0; for (int s = 0; s < N; ++s) { coefA += diffA[s]; coefB += diffB[s]; long long F = coefA * s + coefB; if (s > 0) { int x = p[N - s]; inv += 2LL * x - (N - 1); } if (2LL * inv <= F) { answer.push_back(s); } } cout << answer.size() << '\n'; for (size_t i = 0; i < answer.size(); ++i) { if (i) cout << ' '; cout << answer[i]; } cout << '\n'; } return 0; }
#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 i = 0; i < N; ++i) cin >> p[i]; // Initial inversion count of p. vector<int> bit(N + 1, 0); auto bit_add = [&](int idx) { while (idx <= N) { bit[idx]++; idx += idx & -idx; } }; auto bit_sum = [&](int idx) { int res = 0; while (idx > 0) { res += bit[idx]; idx -= idx & -idx; } return res; }; long long inv = 0; for (int i = 0; i < N; ++i) { inv += i - bit_sum(p[i]); bit_add(p[i]); } long long S = 0; int C = 0; for (int i = 0; i < N; ++i) { int pos = i + 1; S += llabs((long long)p[i] - pos); if (pos >= p[i]) ++C; } // G[s] = number of i with 1 <= i < N such that q_s[i] = i + 1. vector<int> G(N, 0); for (int j = 0; j < N; ++j) { int a = p[j]; if (a >= 2) { int s = (a - 2 - j) % N; if (s < 0) s += N; G[s]++; } } vector<int> ans; ans.reserve(N); for (int s = 0; s < N; ++s) { if (2LL * inv <= S) ans.push_back(s); if (s + 1 < N) { int idx = N - 1 - s; int x = p[idx]; long long newS = S + (2LL * C - N) + (2LL * x - N - 2); long long newInv = inv + 2LL * x - N - 1; C = C + G[s] - (x > 1 ? 1 : 0); S = newS; inv = newInv; } } cout << ans.size() << '\n'; for (size_t i = 0; i < ans.size(); ++i) { if (i) cout << ' '; cout << ans[i]; } cout << '\n'; } return 0; }
#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); vector<int> bit(N + 1, 0); auto bitAdd = [&](int idx) { for (int i = idx; i <= N; i += i & -i) ++bit[i]; }; auto bitSum = [&](int idx) { int res = 0; for (int i = idx; i > 0; i -= i & -i) res += bit[i]; return res; }; long long inv0 = 0; for (int i = 0; i < N; ++i) { cin >> p[i]; int x = p[i]; int le = bitSum(x); inv0 += (long long)i - le; bitAdd(x); } vector<long long> diffM(N + 2, 0), diffB(N + 2, 0); auto addLinear = [&](int l, int r, long long m, long long b) { if (l > r) return; diffM[l] += m; diffM[r + 1] -= m; diffB[l] += b; diffB[r + 1] -= b; }; auto addAbs = [&](int l, int r, long long c) { if (l > r) return; if (c < l) { addLinear(l, r, 1, -c); } else if (c > r) { addLinear(l, r, -1, c); } else { int cc = (int)c; addLinear(l, cc - 1, -1, c); addLinear(cc, r, 1, -c); } }; for (int j = 0; j < N; ++j) { long long a = (long long)p[j] - (j + 1); // s such that j + s < N addAbs(0, N - j - 1, a); // s such that j + s >= N int L = N - j; if (L <= N - 1) { addAbs(L, N - 1, a + N); } } vector<int> ans; ans.reserve(N); long long curM = 0, curB = 0; long long invCur = inv0; for (int s = 0; s < N; ++s) { curM += diffM[s]; curB += diffB[s]; long long sumAbs = curM * (long long)s + curB; if (2 * invCur == sumAbs) { ans.push_back(s); } if (s + 1 < N) { int x = p[N - 1 - s]; invCur += 2LL * x - N - 1; } } cout << ans.size() << '\n'; for (size_t i = 0; i < ans.size(); ++i) { if (i) cout << ' '; cout << ans[i]; } cout << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; struct Fenwick { int n; vector<int> bit; explicit Fenwick(int n) : n(n), bit(n + 1, 0) {} void add(int index) { for (; index <= n; index += index & -index) { ++bit[index]; } } int prefixSum(int index) const { int result = 0; for (; index > 0; index -= index & -index) { result += bit[index]; } return result; } }; 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. Fenwick fenwick(n); long long inversions = 0; for (int i = 0; i < n; ++i) { inversions += static_cast<long long>(i) - fenwick.prefixSum(p[i]); fenwick.add(p[i]); } // displacement = sum |p_i - i| = 2 * f(p). long long displacement = 0; for (int i = 0; i < n; ++i) { displacement += llabs(static_cast<long long>(p[i]) - (i + 1)); } /* countGE is the number of elements whose current position is at least their value. deltaCount[s] is the change in countGE when the permutation is shifted right by s positions. */ vector<int> deltaCount(n + 1, 0); int countGE = 0; for (int j = 1; j <= n; ++j) { int v = p[j - 1]; if (j >= v) { ++countGE; } if (j < v) { // Reaches position v before wrapping. ++deltaCount[v - j]; // Wraps from n to 1 and no longer satisfies pos >= v. --deltaCount[n - j + 1]; } else if (j == v) { if (v > 1) { --deltaCount[n - j + 1]; } } else { // j > v if (v > 1) { // Loses the condition while wrapping ... --deltaCount[n - j + 1]; // ... and regains it when its position becomes v. ++deltaCount[n + v - j]; } } } vector<int> answer; if (2 * inversions <= displacement) { answer.push_back(0); } for (int s = 1; s < n; ++s) { // The element moved from the end to the front. int x = p[n - s]; // Use countGE for the permutation before this shift. displacement += 2LL * (countGE + x - n - 1); inversions += 2LL * x - n - 1; countGE += deltaCount[s]; if (2 * inversions <= displacement) { answer.push_back(s); } } cout << answer.size() << '\n'; for (int i = 0; i < static_cast<int>(answer.size()); ++i) { if (i > 0) { cout << ' '; } cout << answer[i]; } cout << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; struct Fenwick { int n; vector<int> bit; explicit Fenwick(int n) : n(n), bit(n + 1, 0) {} void add(int index) { for (; index <= n; index += index & -index) { ++bit[index]; } } int sum(int index) const { int result = 0; for (; index > 0; index -= index & -index) { result += bit[index]; } return result; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<int> p(N + 1); for (int i = 1; i <= N; ++i) { cin >> p[i]; } /* b[s] = number of positions i < N such that q_s[i] = i + 1, where q_s is p shifted right by s. The element p[j] is at position p[j] - 1 in exactly the shift s = (p[j] - 1 - j) mod N. */ vector<int> b(N, 0); for (int j = 1; j <= N; ++j) { if (p[j] > 1) { int s = (p[j] - 1 - j) % N; if (s < 0) s += N; ++b[s]; } } Fenwick fw(N); long long inversions = 0; long long displacement = 0; int C = 0; // Number of positions i with p[i] <= i. for (int i = 1; i <= N; ++i) { inversions += (i - 1LL) - fw.sum(p[i]); fw.add(p[i]); displacement += llabs(1LL * p[i] - i); if (p[i] <= i) ++C; } vector<int> answer; answer.reserve(N); for (int s = 0; s < N; ++s) { if (2LL * inversions == displacement) { answer.push_back(s); } if (s == N - 1) break; // The last element of the current rotation is p[N - s]. int x = p[N - s]; // Update the inversion count after moving x to the front. inversions += 2LL * x - N - 1; // Update the total displacement. displacement += llabs(1LL * x - 1) - llabs(1LL * x - N) + 2LL * C - N - 1; // Update C for the next rotation. C += -1 + (x == 1 ? 1 : 0) + b[s]; } cout << answer.size() << '\n'; for (int i = 0; i < (int)answer.size(); ++i) { if (i) cout << ' '; cout << answer[i]; } cout << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; struct Fenwick { int n; vector<int> tree; explicit Fenwick(int n) : n(n), tree(n + 1, 0) {} void add(int index) { for (; index <= n; index += index & -index) { ++tree[index]; } } int prefixSum(int index) const { int result = 0; for (; index > 0; index -= index & -index) { result += tree[index]; } return result; } }; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int n; cin >> n; vector<int> p(n + 1), pos(n + 1); Fenwick bit(n); long long inversions = 0; for (int i = 1; i <= n; ++i) { cin >> p[i]; pos[p[i]] = i; // Count previous elements greater than p[i]. inversions += (i - 1) - bit.prefixSum(p[i]); bit.add(p[i]); } // Difference arrays for adding a function slope * s + intercept // to an interval of shifts. vector<long long> diffSlope(n + 1, 0); vector<long long> diffIntercept(n + 1, 0); auto addRange = [&](int left, int right, long long slope, long long intercept) { if (left > right) return; diffSlope[left] += slope; diffSlope[right + 1] -= slope; diffIntercept[left] += intercept; diffIntercept[right + 1] -= intercept; }; for (int value = 1; value <= n; ++value) { int a = pos[value]; // No wrap: the new position is a + s. // Its positive displacement is value - a - s. int right = min(n - a, value - a - 1); addRange(0, right, -1, 1LL * value - a); // Wrap: the new position is a + s - n. // Its positive displacement is n + value - a - s. int left = n - a + 1; right = min(n - 1, n + value - a - 1); addRange(left, right, -1, 1LL * n + value - a); } vector<long long> f(n); long long slope = 0, intercept = 0; for (int s = 0; s < n; ++s) { slope += diffSlope[s]; intercept += diffIntercept[s]; f[s] = slope * s + intercept; } vector<int> answer; answer.reserve(n); for (int s = 0; s < n; ++s) { if (inversions <= f[s]) { answer.push_back(s); } if (s + 1 < n) { // Shifting right moves the last element x to the front. // In q_s, the last element is p[n - s]. int x = p[n - s]; inversions += 2LL * x - n - 1; } } cout << answer.size() << '\n'; for (int i = 0; i < static_cast<int>(answer.size()); ++i) { if (i) cout << ' '; cout << answer[i]; } cout << '\n'; } return 0; }