This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("Ofast")
#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];
struct dsu {
vector<int> g, sz;
dsu(int n) {
g = sz = vector<int>(n + 10);
iota(AI(g), 0);
fill(AI(sz), 1);
}
int F(int i) { return i == g[i] ? i : g[i] = F(g[i]); }
void M(int a, int b) {
a = F(a), b = F(b);
if (a == b) return;
if (sz[a] < sz[b]) swap(a, b);
sz[a] += sz[b], g[b] = a;
}
int operator ()(int i) { return F(i); }
};
pair<int,int> solve_dp(int cost) {
DE(cost);
vector<int> ok(n + 10), cur_nxt(n + 10);
vector<int> ld(n + 10), rd(n + 10), dif(n + 10), cc(n + 10);
dsu D(n);
iota(AI(ld), 0), iota(AI(rd), 0);
int diff_sum = 0, E = n, F = n + 1;
// index, a[i] - a[i-1], cnt[i]
auto mg = [&](int ind, int nxt) {
dif[ind] = dif[nxt];
cc[ind] = cc[nxt];
ld[nxt] = ld[ind];
rd[ind] = rd[nxt];
D.M(ind, nxt);
};
auto pops = [&](int ind) {
if (D(ind) == D(F)) return;
while (ind <= E) {
ind = D(ind);
int lst = D( ld[ind]-1 ), nxt = D( rd[ind]+1 );
int cl = cc[ lst ];
int d = dif[ ind ], cr = cc[ ind ];
if (d > 0 || (d == 0 && cr >= cl)) {
if (ind == D(E)) {
diff_sum -= d;
E = ld[ind]-1;
break;
}
else {
dif[nxt] += dif[ind];
mg(ind, nxt); //
}
}
else break;
}
};
// for all value >= index will be add 1
auto mono_add = [&](int ind) {
if (ind > E) return;
++diff_sum;
++dif[ D(ind) ];
pops(ind);
};
auto mono_push_front = [&](int ind, int dp, int cnt) {
F = ind;
dif[ind] = dp;
cc[ind] = cnt;
if (ind == n) {
diff_sum = dp;
return;
}
int b = D(ind+1);
dif[b] -= dp;
pops(ind+1);
};
auto mono_get = [&]() {
return cc[D(E)];
};
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]) {
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 (i != n) {
int c = mono_get();
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]);
}
return make_pair(cnt[1], dp[1]);
}
int32_t main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> D >> T; ++D;
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 'std::pair<int, int> solve_dp(int)':
prison.cpp:15:17: warning: statement has no effect [-Wunused-value]
15 | #define DE(...) 0
| ^
prison.cpp:48:2: note: in expansion of macro 'DE'
48 | DE(cost);
| ^~
prison.cpp: In function 'int32_t main()':
prison.cpp:154:11: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
154 | 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... |