我的这份代码在 usaco 上能够通过,而洛谷只有 6 分:
#include <bits/stdc++.h>
using namespace std;
const int kMaxN = 1e5 + 5;
class BIT {
public:
void upd(int x, int v) {
for (; x <= n; x += x & -x) {
c[x] += v;
}
}
int qry(int x) {
int ret = 0;
for (; x; x -= x & -x) {
ret += c[x];
}
return ret;
}
BIT() {}
BIT(int _n) : n(_n) {}
private:
int n, c[kMaxN];
} b;
int n, k, idx, tot;
int a[kMaxN], c[kMaxN];
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
b = BIT(n);
a[n + 1] = n + 1;
for (int i = n; i; --i) {
if (a[i] < a[i + 1]) {
b.upd(a[i], 1), ++tot;
} else {
idx = i; break ;
}
}
for (int i = 1; i <= idx; ++i) {
int x = b.qry(a[i]) + 1;
++tot;
c[++k] = n - tot + x - 1;
b.upd(a[i], 1);
}
cout << k << endl;
for (int i = 1; i <= k; ++i) {
cout << c[i];
if (i != k) cout << ' ';
}
return 0;
}