标签:cto namespace test std global false https wap ted
题目链接:https://codeforces.com/contest/1427/problem/D
给出一个大小为 \(n\) 的排列,每次操作可以将 \(n\) 个数分为 \(1 \sim n\) 个连续份,然后将对称的份两两交换,试给出在 \(n\) 次操作内将排列排为升序的操作过程。
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n), pos(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
--a[i];
}
vector<vector<int>> ans;
while (not is_sorted(a.begin(), a.end())) {
for (int i = 0; i < n; i++) {
pos[a[i]] = i;
}
for (int i = 1; i < n; i++) {
if (pos[i] < pos[i - 1]) {
int l = pos[i];
int r = pos[i - 1];
int mid = l;
while (a[mid + 1] == a[mid] + 1) ++mid;
ans.push_back({l, mid - l + 1, r - mid, n - r - 1});
vector<int> v;
for (int i = r + 1; i < n; i++) v.push_back(a[i]);
for (int i = mid + 1; i < r + 1; i++) v.push_back(a[i]);
for (int i = l; i < mid + 1; i++) v.push_back(a[i]);
for (int i = 0; i < l; i++) v.push_back(a[i]);
a.swap(v);
break;
}
}
}
cout << ans.size() << "\n";
for (auto &v : ans) {
while (v.back() == 0) v.pop_back();
while (v.front() == 0) v.erase(v.begin());
cout << v.size() << "\n";
for (int i = 0; i < int(v.size()); i++) {
cout << v[i] << " \n"[i == int(v.size()) - 1];
}
}
return 0;
}
Codeforces Global Round 11 D. Unshuffling a Deck(构造/相邻逆序对)
标签:cto namespace test std global false https wap ted
原文地址:https://www.cnblogs.com/Kanoon/p/13812520.html