This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define mnto(x, y) x = min(x, (__typeof__(x)) y)
#define mxto(x, y) x = max(x, (__typeof__(x)) y)
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define MP make_pair
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define MT make_tuple
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define pb emplace_back
typedef vector<int> vi;
typedef vector<ii> vii;
#define INF 1000000005
#define LINF 1000000000000000005
#define MOD 1000000007
#define MAXN 2000005
#define MAXL 22
int n, d, t;
int ti[MAXN];
ll dp[2][MAXN];
int lst[MAXN];
ii p[MAXL][MAXN];
int id[MAXN * 2], val[MAXN * 2];
int fw[MAXN];
void incre(int p, int v) {
for (; p <= n; p += p & -p) mxto(fw[p], v);
}
int query(int p) {
int res = 0;
for (; p > 0; p -= p & -p) mxto(res, fw[p]);
return res;
}
ll cost(int l, int r) {
ll res = 0;
int cur = id[l];
RREP (k, MAXL - 1, 0) {
if (p[k][cur].FI != -1 && val[p[k][cur].FI] <= r) {
res += p[k][cur].SE;
cur = p[k][cur].FI;
}
}
assert(p[0][cur].FI == -1 || val[p[0][cur].FI] > r);
if (p[0][cur].SE > 0) {
res += r - val[cur] + 1;
}
return res;
}
void dnc(int i, int lo, int hi, int optl, int optr) {
if (lo > hi) return;
int j = lo + hi >> 1;
int opt = -1;
dp[i % 2][j] = LINF;
REP (k, max(1, optl), min(optr + 1, j)) {
if (dp[i % 2][j] > dp[(i + 1) % 2][k] + cost(k + 1, j)) {
dp[i % 2][j] = dp[(i + 1) % 2][k] + cost(k + 1, j);
opt = k;
}
}
dnc(i, lo, j - 1, optl, opt);
dnc(i, j + 1, hi, opt, optr);
}
int main() {
scanf("%d%d%d", &n, &d, &t);
REP (i, 1, n + 1) {
scanf("%d", &ti[i]);
fw[i] = -1;
}
RREP (i, n, 1) {
int r = min(n, t - ti[i] + i);
if (ti[i] > t) r = -1;
if (r != -1) {
mxto(r, query(r));
}
incre(i, r);
lst[i] = r;
}
int cnt = n + 1;
id[n + 1] = n + 1;
val[n + 1] = n + 1;
RREP (i, n, 1) {
id[i] = i;
val[i] = i;
if (lst[i] != -1) {
if (lst[i] == i) {
id[i] = ++cnt;
val[cnt] = i;
p[0][i] = MP(id[i + 1], 0);
p[0][cnt] = MP(i + 1, 1);
} else {
p[0][i] = MP(lst[i], lst[i] - i + 1);
}
} else {
p[0][i] = MP(id[i + 1], 0);
}
}
p[0][n + 1] = MP(-1, 0);
REP (k, 1, MAXL) {
REP (i, 1, cnt + 2) {
if (p[k - 1][i].FI == -1) {
p[k][i] = p[k - 1][i];
} else {
p[k][i] = p[k - 1][p[k - 1][i].FI];
p[k][i].SE += p[k - 1][i].SE;
}
}
}
REP (i, 1, n + 1) {
dp[0][i] = cost(1, i);
}
REP (i, 1, d + 1) {
dnc(i, 1, n, 1, n);
}
printf("%lld\n", dp[d % 2][n]);
return 0;
}
/*
5 1 42
13 37 47 11 42
5 2 5
1 9 4 6 7
*/
Compilation message (stderr)
prison.cpp: In function 'void dnc(int, int, int, int, int)':
prison.cpp:63:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
63 | int j = lo + hi >> 1;
| ~~~^~~~
prison.cpp: In function 'int main()':
prison.cpp:77:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
77 | scanf("%d%d%d", &n, &d, &t);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
prison.cpp:79:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
79 | scanf("%d", &ti[i]);
| ~~~~~^~~~~~~~~~~~~~
# | 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... |