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;
using ll = long long;
#define pb emplace_back
#define AI(i) begin(i), end(i)
template<class T> bool chmin(T &a, T b) { return b < a && (a = b, true); }
template<class T> bool chmax(T &a, T b) { return a < b && (a = b, true); }
#ifdef KEV
#define DE(args...) kout("[ " + string(#args) + " ] = ", args)
void kout() { cerr << endl; }
template<class T, class ...U> void kout(T a, U ...b) { cerr << a << ' ', kout(b...); }
template<class T> void debug(T l, T r) { while (l != r) cerr << *l << " \n"[next(l)==r], ++l; }
#else
#define DE(...) 0
#define debug(...) 0
#endif
const int MAX_N = 4005, inf = 1e9;
int dp[MAX_N], cnt[MAX_N];
int dp2[MAX_N][MAX_N];
int n, D, T;
// i can activate [i, nxt[i])
int s[MAX_N], nxt[MAX_N];
pair<int,int> solve_dp(int cost) {
vector<int> ok(n + 10);
for (int i = n;i >= 1;--i) {
for (int j = i;j < nxt[i];++j)
ok[j] = true;
dp[i] = accumulate(begin(ok)+i, end(ok), 0) + cost;
cnt[i] = 1;
int sum = 0;
for (int k = i+1;k <= n;++k) {
sum += ok[k-1];
if (chmin(dp[i], cost + sum + dp[k]))
cnt[i] = cnt[k]+1;
if (dp[i] == cost + sum + dp[k])
chmin(cnt[i], cnt[k]+1);
}
}
return make_pair(cnt[1], dp[1]);
}
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> D >> T; ++D;
// Just to be faster
assert(n < MAX_N);
for (int i = 1;i <= n;++i) {
cin >> s[i];
nxt[i] = min(n+1, max(i, i + T - s[i] + 1));
}
//{
// auto [c, w] = solve_dp(0);
// DE(c, w);
// if (c <= D) {
// cout << w << '\n';
// return 0;
// }
//}
int l = 0, r = n, mid;
while (l < r) {
mid = l + r >> 1;
auto [c, w] = solve_dp(mid);
if (c > D)
l = mid+1;
else
r = mid;
}
auto [c, w] = solve_dp(l);
cout << w - l * D << '\n';
}
Compilation message (stderr)
prison.cpp: In function 'int32_t main()':
prison.cpp:76:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
76 | mid = l + r >> 1;
| ~~^~~
# | 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... |