This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/* I do it for the glory */
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<double, int> pii;
#define SZ(x) (int) x.size()
#define F first
#define S second
#define lc id << 1
#define rc lc | 1
const int N = 1e6 + 10, MOD = 1e9 + 7; const double eps = 1e-14;
int R[N], t[N], n, T, D; int seg[N << 2], dp[N][100];
void bld(int x, int id = 1, int l = 1, int r = n + 1) {
if (r - l < 2) { seg[id] = dp[l][x] + l; return; }
int md = (l + r) >> 1;
bld(x, lc, l, md), bld(x, rc, md, r);
seg[id] = min(seg[lc], seg[rc]);
}
int get(int ql, int qr, int id = 1, int l = 1, int r = n + 1) {
if (qr <= l || r <= ql) return MOD;
if (ql <= l && r <= qr) { return seg[id]; }
int md = (l + r) >> 1;
return min(get(ql, qr, lc, l, md), get(ql, qr, rc, md, r));
}
int main() {
scanf("%d%d%d", &n, &D, &T);
//assert(n <= N);
for (int i = 1; i <= n; i++)
scanf("%d", &t[i]);
for (int i = n; i; i--) {
for (R[i] = i + 1; R[i] <= n && t[R[i]] - R[i] > t[i] - i; R[i] = R[R[i]]);
}
for (int i = n; i; i--) {
if (R[i] <= min(n, i + T - t[i]))
R[i] = R[R[i]];
else R[i] = min(n, i + T - t[i]);
if (R[i] < i) R[i] = -1;
}
for (int i = n; i; i--) {
if (R[i] < i) dp[i][0] = dp[i + 1][0];
else dp[i][0] = (R[i] - i + 1) + dp[R[i] + 1][0];
}
for (int j = 1; j <= D; j++) {
bld(j - 1);
for (int i = n; i; i--) {
if (R[i] < i) dp[i][j] = min(dp[i + 1][j - 1], dp[i + 1][j]);
else {
int x = get(i + 1, R[i] + 2);
dp[i][j] = min(x - i, dp[R[i] + 1][j] + R[i] - i + 1);
}
}
}
printf("%d\n", dp[1][D]);
return 0;
}
Compilation message (stderr)
prison.cpp: In function 'int main()':
prison.cpp:33:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | scanf("%d%d%d", &n, &D, &T);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
prison.cpp:36:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | scanf("%d", &t[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... |