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;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define pil pair<int,long long>
#define pll pair<long long,long long>
#define eb emplace_back
const int maxn=3e5+9;
int a[maxn];
int n;
void nen(){
vector<int>tmp;
tmp.pb(-1);
for1(i,1,n)tmp.pb(a[i]);
sort(all(tmp));
map<int,int>rel;
for1(i,1,sz(tmp)-1){
if (tmp[i]>tmp[i-1])rel[tmp[i]]=rel[tmp[i-1]]+1;
}
for1(i,1,n)a[i]=rel[a[i]];
}
pii st[maxn*4];
int best[maxn],f[maxn],lazy[maxn*4],mx[maxn*4];
pii combine(const pii &p, const pii &q){
if (p.fi>=q.fi)return q;
return p;
}
void push(int id){
if (lazy[id]){
lazy[id*2]=max(lazy[id*2],lazy[id]);
lazy[id*2+1]=max(lazy[id*2+1],lazy[id]);
st[id*2].fi=max(st[id*2].fi,lazy[id]);
st[id*2+1].fi=max(st[id*2+1].fi,lazy[id]);
lazy[id]=0;
}
}
void build(int id,int l,int r){
st[id]={n+1,l};
if (l==r)return;
int mid=(l+r)/2;
build(id*2,l,mid);
build(id*2+1,mid+1,r);
}
void update(int id,int l,int r,int u,int val,int v1){
if (l>u||r<u)return;
if (l==r){
lazy[id]=0;
st[id]={val,u};
mx[id]=v1;
return;
}
int mid=(l+r)/2;
push(id);
update(id*2,l,mid,u,val,v1);
update(id*2+1,mid+1,r,u,val,v1);
st[id]=combine(st[id*2],st[id*2+1]);
mx[id]=max(mx[id*2],mx[id*2+1]);
}
void range(int id,int l,int r,int u,int v,int val){
if (l>v||r<u||u>v)return;
if (u<=l&&r<=v){
st[id].fi=max(st[id].fi,val);
lazy[id]=max(lazy[id],val);
return;
}
int mid=(l+r)/2;
push(id);
range(id*2,l,mid,u,v,val);
range(id*2+1,mid+1,r,u,v,val);
st[id]=combine(st[id*2],st[id*2+1]);
}
int get(int id,int l,int r,int u,int v){
if (l>v||r<u||u>v)return 0;
if (u<=l&&r<=v)return mx[id];
int mid=(l+r)/2;
return max(get(id*2,l,mid,u,v),get(id*2+1,mid+1,r,u,v));
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
int d;
cin>>n>>d;
for1(i,1,n){
cin>>a[i];
}
nen();
build(1,1,n);
multiset<int>sav;
multiset<int>::iterator it;
sav.insert(0);
int ans=0;
for1(i,1,n){
while (st[1].fi<i-d){
best[st[1].se]=0;
update(1,1,n,st[1].se,n+1,0);
}
f[i]=1+get(1,1,n,1,a[i]-1);
if (best[a[i]]<f[i]){
best[a[i]]=f[i];
update(1,1,n,a[i],i,f[i]);
}
range(1,1,n,a[i],n,i);
ans=max(ans,f[i]);
}
cout<<ans;
}
# | 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... |