이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 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());
SEGTMX 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-1, ind + d) + 1);
nxt[ind] = max(nxt[ind], min(ind + d, n-1));
seg.update(1, 1, n, ind + 1, nxt[ind]);
}
vector<int> prenxt(n);
prenxt[0] = nxt[0];
for(int i = 1; i < n; i++){
prenxt[i] = max(prenxt[i-1], nxt[i]);
}
vector<int> lp(n);
int ind = n-1;
for(int i = n-1; i >= 0; i--){
while(ind >= 0 && prenxt[ind] >= i) ind--;
lp[i] = ind + 1;
}
SEGTMX segmx;
segmx.init(n);
vector<int> dp(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... |