This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/**
* author: wxhtzdy
* created: 28.11.2022 15:11:47
**/
#include <bits/stdc++.h>
using namespace std;
int readint() {
char c = getchar();
while (c < '0' || c > '9') {
c = getchar();
}
int x = 0;
while (c >= '0' && c <= '9') {
x = x * 10 + (c - '0');
c = getchar();
}
return x;
}
const int N = 2000005;
int n, d, t;
int a[N];
int add[4 * N];
int mx[4 * N];
int idx[4 * N];
pair<int, int> mn[4 * N];
int prv[N];
void Build(int x, int l, int r) {
idx[x] = l;
if (l == r) {
mn[x] = make_pair(prv[l] == -1 ? n + 1 : prv[l], l);
return;
}
int y = (l + r) >> 1;
int z = x + 2 * (y - l + 1);
Build(x + 1, l, y);
Build(z, y + 1, r);
mn[x] = min(mn[x + 1], mn[z]);
}
void Modify(int x, int l, int r, int ll, int rr, int v) {
if (ll <= l && r <= rr) {
add[x] += v;
mx[x] += v;
return;
}
if (l > rr || r < ll) {
return;
}
int y = (l + r) >> 1;
int z = x + 2 * (y - l + 1);
if (add[x] != 0) {
mx[x + 1] += add[x];
mx[z] += add[x];
add[x + 1] += add[x];
add[z] += add[x];
add[x] = 0;
}
Modify(x + 1, l, y, ll, rr, v);
Modify(z, y + 1, r, ll, rr, v);
if (mx[x + 1] >= mx[z]) {
mx[x] = mx[x + 1];
idx[x] = idx[x + 1];
} else {
mx[x] = mx[z];
idx[x] = idx[z];
}
}
pair<int, int> Query(int x, int l, int r, int ll, int rr) {
if (ll <= l && r <= rr) {
return mn[x];
}
int y = (l + r) >> 1;
int z = x + 2 * (y - l + 1);
if (rr <= y) {
return Query(x + 1, l, y, ll, rr);
} else if (ll > y) {
return Query(z, y + 1, r, ll, rr);
} else {
return min(Query(x + 1, l, y, ll, rr), Query(z, y + 1, r, ll, rr));
}
}
void Remove(int x, int l, int r, int i) {
if (l == r) {
mn[x] = make_pair(n + 1, l);
return;
}
int y = (l + r) >> 1;
int z = x + 2 * (y - l + 1);
if (i <= y) {
Remove(x + 1, l, y, i);
} else {
Remove(z, y + 1, r, i);
}
mn[x] = min(mn[x + 1], mn[z]);
}
int main() {
n = readint();
d = readint();
t = readint();
for (int i = 0; i < n; i++) {
a[i] = readint();
}
vector<int> stk;
for (int i = 0; i < n; i++) {
while (!stk.empty() && a[stk.back()] + i - stk.back() > t) {
stk.pop_back();
}
while (!stk.empty() && a[stk.back()] - stk.back() > a[i] - i) {
stk.pop_back();
}
prv[i] = (a[i] <= t ? i : (stk.empty() ? -1 : stk.back()));
stk.push_back(i);
}
Build(0, 0, n - 1);
int ans = 0;
for (int i = 0; i < n; i++) {
if (prv[i] != -1) {
ans += 1;
if (prv[i] < i) {
Modify(0, 0, n - 1, prv[i], i - 1, +1);
}
}
}
while (d--) {
int i = idx[0];
ans -= mx[0];
while (true) {
pair<int, int> p = Query(0, 0, n - 1, i + 1, n - 1);
if (p.first <= i) {
Modify(0, 0, n - 1, p.first, p.second - 1, -1);
Remove(0, 0, n - 1, p.second);
} else {
break;
}
}
}
printf("%d", ans);
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |