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>
#define pb push_back
#define int long long
using namespace std;
const int mod = 998244353;
const int inf = 1e9 + 20;
const int N = 1e5+5;
struct SEGT{
vector<int> tree;
void init(int n){
tree.assign(4*n, inf);
}
void update(int ind, int l, int r, int pos, int val){
if(l == r){
tree[ind] = val;
}
else{
int m = (l + r)/2;
if(pos <= m) update(ind*2, l, m, pos, val);
else update(ind*2+1, m+1, r, pos, val);
tree[ind] = min(tree[ind*2], tree[ind*2+1]);
}
}
int query(int ind, int l, int r, int ql, int qr){
if(l > r || l > qr || r < ql) return inf;
if(l >= ql && r <= qr) return tree[ind];
else{
int m = (l + r)/2;
return min(query(ind*2, l, m, ql, qr), query(ind*2+1, m+1, r, ql, qr));
}
}
};
struct SEGTMX{
vector<int> tree;
void init(int n){
tree.assign(4*n, 0);
}
void update(int ind, int l, int r, int pos, int val){
if(l == r){
tree[ind] = val;
}
else{
int m = (l + r)/2;
if(pos <= m) update(ind*2, l, m, pos, val);
else update(ind*2+1, m+1, r, pos, val);
tree[ind] = max(tree[ind*2], tree[ind*2+1]);
}
}
int query(int ind, int l, int r, int ql, int qr){
if(l > r || l > qr || r < ql) return 0;
if(l >= ql && r <= qr) return tree[ind];
else{
int m = (l + r)/2;
return max(query(ind*2, l, m, ql, qr), query(ind*2+1, m+1, r, ql, qr));
}
}
};
int32_t main(){
cin.tie(0); cout.tie(0);
ios_base::sync_with_stdio(false);
int n, d;
cin>>n>>d;
vector<int> a(n);
vector<pair<int, int> > b(n);
for(int i = 0; i < n; i++){
cin>>a[i];
b[i] = {a[i], -i};
}
sort(b.begin(), b.end());
SEGT seg;
seg.init(n);
vector<int> nxt(n);
for(int i = 0; i < n; i++){
int ind = -b[i].second;
nxt[ind] = seg.query(1, 1, n, ind + 1, min(n, ind + d + 1));
if(nxt[ind] == inf) nxt[ind] = min(ind + d, n-1);
seg.update(1, 1, n, ind + 1, nxt[ind]);
}
vector<int> lp(n);
int ind = n-1;
for(int i = n-1; i >= 0; i--){
while(ind >= 0 && nxt[ind] >= i) ind--;
lp[i] = ind + 1;
}
vector<int> dp(n, 1);
SEGTMX segmx;
segmx.init(n);
for(int i = 0; i < n; i++){
int ind = -b[i].second;
int l = lp[ind] + 1, r = ind + 1;
int val = segmx.query(1, 1, n, l, r);
dp[ind] = val + 1;
segmx.update(1, 1, n, ind + 1, val + 1);
}
int ans = 0;
for(int i = 0; i < n; i++){
ans = max(ans, dp[i]);
}
cout<<ans<<endl;
return 0;
}
# | 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... |