题解:P11455 [USACO24DEC] Cowdepenence G
VitrelosTiAFO · · 题解
对于固定
当
当
由均值取等,
const int N = 1e5 + 5;
int n, B, a[N], ds[N]; vector <int> p[N];
void update(int l, int r, int k) { ds[l] += k; ds[r + 1] -= k; }
int calc(int c, int x) {
int lst = -1e9, ans = 0;
for (int t : p[c]) if (t - lst > x) ans++, lst = t;
return ans;
}
signed main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n; B = sqrt(n * __lg(n));
for (int i = 1; i <= n; i++) cin >> a[i], p[a[i]].push_back(i);
for (int i = 1; i <= n; i++) if (p[i].size()) {
for (int x = 1; x <= B; x++) update(x, x, calc(i, x));
for (int x = B + 1; x <= n; ) {
int l = x, r = n, ans = l, t = calc(i, x);
while (l <= r) {
int mid = (l + r) >> 1;
if (calc(i, mid) == t) l = mid + 1, ans = mid;
else r = mid - 1;
}
update(x, ans, t); x = ans + 1;
}
}
for (int i = 1; i <= n; i++) cout << (ds[i] += ds[i - 1]) << '\n';
return 0;
}