#include <bits/stdc++.h>
#define long long long
using namespace std;
void init(){
#define task "main"
if (fopen(task".INP","r")){
freopen(task".INP","r",stdin);
freopen(task".OUT","w",stdout);
}
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int N;
long D;
vector<long> A;
vector<int> dp, rk;
vector<long> vs;
vector< multiset<int> > ms;
vector<int> st;
// segment‑tree point‑update, range‑max query
void update(int o, int l, int r, int p, int v){
if(l==r){
if(v<0) st[o]=0;
else st[o]=v;
return;
}
int m=(l+r)>>1;
if(p<=m) update(o<<1,l,m,p,v);
else update(o<<1|1,m+1,r,p,v);
st[o]=max(st[o<<1],st[o<<1|1]);
}
int query(int o, int l, int r, int L, int R){
if(L>R) return 0;
if(L<=l&&r<=R) return st[o];
int m=(l+r)>>1, res=0;
if(L<=m) res=max(res, query(o<<1,l,m,L,R));
if(R> m) res=max(res, query(o<<1|1,m+1,r,L,R));
return res;
}
int main(){
init();
cin>>N>>D;
A.resize(N+1);
for(int i=1;i<=N;i++) cin>>A[i];
// compress
vs = A;
sort(vs.begin()+1, vs.end());
vs.erase(unique(vs.begin()+1, vs.end()), vs.end());
int M = (int)vs.size()-1;
rk.resize(N+1);
for(int i=1;i<=N;i++){
rk[i] = int(lower_bound(vs.begin()+1, vs.end(), A[i]) - vs.begin());
}
dp.assign(N+1,0);
ms.assign(M+1, multiset<int>() );
st.assign(4*(M+2), 0);
int ans = 1;
// we will insert j into structure when we pass it, and remove when it's too far
for(int i=1, l=1; i<=N; i++){
// add j = i-1
if(i>1){
int j = i-1, rj = rk[j];
ms[rj].insert(dp[j]);
update(1,1,M,rj, *ms[rj].rbegin());
}
// remove j = i-D-1
if(i - l > D){
int j = l, rj = rk[j];
auto it = ms[rj].find(dp[j]);
ms[rj].erase(it);
if(ms[rj].empty()) update(1,1,M,rj, -1);
else update(1,1,M,rj, *ms[rj].rbegin());
l++;
}
int ri = rk[i];
// case 1: lấy từ những j có A[j]<A[i]
int c1 = query(1,1,M,1,ri-1) + 1;
// case 2: lấy từ những j có A[j]>=A[i]
int c2 = query(1,1,M,ri,M);
dp[i] = max(c1, c2);
if(dp[i]==0) dp[i]=1; // bắt đầu mới tại i
if(i==N) ans = dp[i];
}
cout<<ans<<"\n";
return 0;
}
Compilation message (stderr)
Main.cpp: In function 'void init()':
Main.cpp:8:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
8 | freopen(task".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:9:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
9 | freopen(task".OUT","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |