题解:P9291 [ROI 2018] Quick sort
Egg_eating_master · · 题解
这是一个并不正经的乱搞做法。
考虑
注意到一次操作的左端点并不一定需要是
容易证明,在随机数据下,这个做法可以让总次数期望乘
::::info[代码]
#include<bits/stdc++.h>
#define pii pair<int, int>
#define fi first
#define se second
#define mkp make_pair
using namespace std;
const int maxn = 3005;
int n;
int a[maxn], p[maxn];
vector<pii> ans;
void qsort(int l, int r) {
ans.push_back(mkp(l, r));
vector<int> tmp;
for (int i = l + 1; i <= r; i += 2) tmp.push_back(a[i]);
for (int i = l; i <= r; i += 2) tmp.push_back(a[i]);
for (int i = l; i <= r; i++) p[a[i] = tmp[i - l]] = i;
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i], p[a[i]] = i;
for (int i = n; i >= 1; i--)
while (p[i] != i) {
int x = i, y = p[i];
while (x > 1 && p[x - 1] % 2 == p[i] % 2) y = min(y, p[--x]);
qsort(y, i);
}
cout << ans.size() << '\n';
for (auto x : ans) cout << x.fi << ' ' << x.se << '\n';
return 0;
}
::::