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;
const int inf = numeric_limits<int> :: max();
struct ST{
vector<int> t, p;
int n;
ST(int ns){
n = ns;
t.resize(4 * n);
p.resize(4 * n);
}
void push(int& v, int& vv){
if (!p[v]) return;
p[vv] += p[v]; t[vv] += p[v];
p[vv + 1] += p[v]; t[vv + 1] += p[v];
p[v] = 0;
}
void add(int v, int tl, int tr, int& l, int& r, int& x){
if (l > tr || r < tl) return;
if (l <= tl && tr <= r){
t[v] += x;
p[v] += x;
return;
}
int tm = (tl + tr) / 2, vv = 2 * v;
push(v, vv);
add(vv, tl, tm, l, r, x);
add(vv + 1, tm + 1, tr, l, r, x);
t[v] = min(t[vv], t[vv + 1]);
}
void add(int l, int r, int x){
if (l > r) return;
add(1, 1, n, l, r, x);
}
int get(int v, int tl, int tr, int& l, int& r){
if (l > tr || r < tl) return inf;
if (l <= tl && tr <= r){
return t[v];
}
int tm = (tl + tr) / 2, vv = 2 * v;
push(v, vv);
return min(get(vv, tl, tm, l, r), get(vv + 1, tm + 1, tr, l, r));
}
int get(int l, int r){
if (l > r) return inf;
return get(1, 1, n, l, r);
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, d, t; cin>>n>>d>>t;
vector<int> a(n + 1);
for (int i = 1; i <= n; i++){
cin>>a[i];
}
vector<int> h(n + 1);
priority_queue<int, vector<int>, greater<int>> pq;
int tt = 0, cnt = 0;
for (int j = 1; j <= n; j++){
pq.push(a[j] - tt);
int m = pq.top() + tt;
cnt += (m <= t);
h[j] = cnt;
tt++;
}
vector<int> r(n + 1);
for (int i = 1; i <= n; i++){
if (a[i] > t) continue;
int m = t - a[i];
r[i] = i + m;
}
const int lg = ceil(log2(n));
vector<vector<int>> sp(n + 1, vector<int>(lg));
for (int i = 1; i <= n; i++){
sp[i][0] = r[i];
}
for (int j = 1; j < lg; j++){
for (int i = 1; i + (1 << j) <= n + 1; i++){
sp[i][j] = max(sp[i][j - 1], sp[i + (1 << (j - 1))][j - 1]);
}
}
vector<int> log(n + 1);
for (int i = 2; i <= n; i++){
log[i] = log[i / 2] + 1;
}
auto check = [&](int x, int y){
int k = log[y - x + 1];
return max(sp[x][k], sp[y - (1 << k) + 1][k]) >= y;
};
vector<int> f(n);
for (int i = 1; i < n; i++){
int j = 2;
while (j <= i + 1 && check(j, i + 1)){
j++;
}
f[i] = j - 1;
}
vector<vector<int>> dp(n + 1, vector<int>(d + 1));
for (int i = 1; i <= n; i++){
dp[i][0] = h[i];
}
for (int k = 1; k <= d; k++){
ST T(n);
for (int i = 1; i <= n; i++){
T.add(1, f[i - 1] - 1, 1);
dp[i][k] = min(dp[i][k - 1], T.get(1, i - 1));
T.add(i, i, dp[i][k - 1]);
}
}
cout<<dp[n][d]<<"\n";
}
# | 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... |