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>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
#define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
#define mod 998244353
#define xx first
#define yy second
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define ll long long
#define pii pair<int,int>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>,rb_tree_tag, tree_order_statistics_node_update> ordered_set;/// find_by_order(x)(x+1th) , order_of_key() (strictly less)
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count());
int n,d,t;
int niz[500005];
int dp[500005];
int ans[500005][2];
int nxt[500005];
int seg[4 * 100005];
int kveri(int node, int l, int r, int levo, int desno){
if(l > r || levo > desno)return 1e9;
if(r < levo || desno < l)return 1e9;
if(l >= levo && r <= desno)return seg[node];
int mid = (l + r) / 2;
return min(kveri(node * 2, l, mid, levo, desno), kveri(node * 2 + 1, mid + 1, r, levo, desno));
}
void init(int node, int l, int r){
if(l == r){
seg[node] = 1e9;
return;
}
int mid = (l + r) / 2;
init(node * 2, l, mid);
init(node * 2 + 1, mid + 1, r);
seg[node] = 1e9;
}
void update(int node, int l, int r, int poz, int val){
if(l == r){
seg[node] = val;
return;
}
int mid = (l + r) / 2;
if(poz <= mid)update(node * 2, l, mid, poz, val);
else update(node * 2 + 1, mid + 1, r, poz, val);
seg[node] = min(seg[node * 2], seg[node * 2 + 1]);
}
int main()
{
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n >> d >> t;
ff(i,1,n){
cin >> niz[i];
niz[i] -= i;
}
d = min(n,d);
stack<int> s;
fb(i,n,1){
if(s.empty()){
nxt[i] = n+1;
s.push(i);
continue;
}
while(!s.empty() && niz[s.top()]>niz[i]){
s.pop();
}
if(s.empty())nxt[i] = n+1;
else{
nxt[i] = s.top();
}
s.push(i);
}
int mn = niz[1];
ff(i,1,n){
mn = min(niz[i], mn);
if(mn <= t-i)ans[i][0]++;
ans[i][0] += ans[i - 1][0];
}
fb(i,n,1){
int kaj = nxt[i] - 1;
int rch = t - (niz[i] + i);
if(rch < 0){
ans[i][1] = ans[i + 1][1];
continue;
}
ans[i][1] = ans[nxt[i]][1] + min(rch + 1, nxt[i]-1-i+1);
}
if(d == 1){
int sol = 1e9;
ff(i,1,n){
sol = min(sol, ans[i][0] + ans[i+1][1]);
}
cout << sol << "\n";
return 0;
}
ff(j,0,d){
if(j == 0){
fb(i,n,1){
dp[i] = ans[i][1];
update(1,1,n,i,i+dp[i]);
}
continue;
}
fb(i,n,1){
dp[i] = dp[nxt[i]];
int r = nxt[i] - 1;
int reach = t - (i + niz[i]);
if(reach < 0){
dp[i] = dp[i + 1];
continue;
}
dp[i] += min(reach+1,nxt[i]-1-i+1);
r = min(r, i + reach);
dp[i] = min(dp[i], kveri(1,1,n,i,r)-i);
}
ff(i,1,n)update(1,1,n,i,i+dp[i]);
}
cout << dp[1] << "\n";
return 0;
}
Compilation message (stderr)
prison.cpp: In function 'int main()':
prison.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
prison.cpp:65:5: note: in expansion of macro 'ff'
65 | ff(i,1,n){
| ^~
prison.cpp:7:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
7 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
| ^
prison.cpp:71:5: note: in expansion of macro 'fb'
71 | fb(i,n,1){
| ^~
prison.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
prison.cpp:87:5: note: in expansion of macro 'ff'
87 | ff(i,1,n){
| ^~
prison.cpp:7:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
7 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
| ^
prison.cpp:92:5: note: in expansion of macro 'fb'
92 | fb(i,n,1){
| ^~
prison.cpp:93:13: warning: unused variable 'kaj' [-Wunused-variable]
93 | int kaj = nxt[i] - 1;
| ^~~
prison.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
prison.cpp:103:9: note: in expansion of macro 'ff'
103 | ff(i,1,n){
| ^~
prison.cpp:6:27: warning: unnecessary parentheses in declaration of 'j' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
prison.cpp:110:5: note: in expansion of macro 'ff'
110 | ff(j,0,d){
| ^~
prison.cpp:7:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
7 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
| ^
prison.cpp:112:13: note: in expansion of macro 'fb'
112 | fb(i,n,1){
| ^~
prison.cpp:7:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
7 | #define fb(i,a,b) for(int (i) = (a); (i) >= (b); --(i))
| ^
prison.cpp:118:9: note: in expansion of macro 'fb'
118 | fb(i,n,1){
| ^~
prison.cpp:6:27: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
6 | #define ff(i,a,b) for(int (i) = (a); (i) <= (b); ++(i))
| ^
prison.cpp:131:9: note: in expansion of macro 'ff'
131 | ff(i,1,n)update(1,1,n,i,i+dp[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... |