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 = 2e6 + 10, inf = 1e9;
int dp[MAX_N], cnt[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), cur_nxt(n + 10);
int diff_sum = 0;
// index, a[i] - a[i-1], cnt[i]
map<int, pair<int,int>> mono;
auto pops = [&](auto it) {
while (it != begin(mono) && it != end(mono)) {
auto pit = prev(it);
auto [_, cl] = pit->second;
auto [d, cr] = it->second;
if (d == 0 && cr >= cl) {
it = mono.erase(it);
continue;
}
if (d > 0) {
if (next(it) == end(mono)) {
diff_sum -= d;
}
else {
next(it)->second.first += d;
}
it = mono.erase(it);
}
else break;
}
};
// for all value >= index will be add 1
auto mono_add = [&](int ind) {
auto it = mono.lower_bound(ind);
if (it == end(mono)) return;
++it->second.first;
++diff_sum;
pops(it);
};
auto mono_push_front = [&](int ind, int dp, int cnt) {
if (mono.empty()) {
mono[ind] = {dp, cnt};
diff_sum = dp;
return;
}
auto &[d, c] = mono.begin()->second;
d -= dp;
mono[ind] = {dp, cnt};
pops(next(begin(mono)));
};
int cnt1 = 0;
for (int i = n;i >= 1;--i) {
cur_nxt[i] = nxt[i];
for (int j = i;j < nxt[i];++j) {
if (ok[j]) {
assert(cur_nxt[j] > j);
chmax(cur_nxt[i], cur_nxt[j]);
j = cur_nxt[j] - 1;
}
else {
ok[j] = true, ++cnt1;
mono_add(j + 1);
}
}
dp[i] = cnt1 + cost;
cnt[i] = 1;
if (mono.size()) {
auto [_, c] = prev(end(mono))->second;
if (chmin(dp[i], cost + diff_sum))
cnt[i] = c + 1;
if (dp[i] == cost + diff_sum)
cnt[i] = c + 1;
}
mono_push_front(i, dp[i], cnt[i]);
// 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));
}
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:139:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
139 | 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... |