This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define ld long double
#define pb push_back
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int N,D;
vector<int> v;
int memo[300010];
int val[300010];
int prev[300010];
const int dim=3000;
vector<int> ma(100000);
struct min_queue{
deque<pair<int,int> >q;
int top(){
return q.front().fi;
}
void insert(int v,int t){
while(q.size() && q.back().fi>=v)q.pop_back();
q.pb({v,t});
}
void pop(int t){
while(q.size() && q.front().se<=t)q.pop_front();
}
}q;
struct fenwick{
const int dim=1<<19;
vector<int> f=vector<int> (dim,0);
void upd(int pos,int val){
pos+=10;
for(int i=pos;i<dim;i+=i&(-i))f[i]=max(f[i],val);
}
int ma(int pos){
pos+=10;
int ans=0;
for(int i=pos;i>0;i-=i&(-i))ans=max(ans,f[i]);
return ans;
}
};
vector<fenwick> z;
signed main(){
ios::sync_with_stdio(false);cin.tie(0);
cin>>N>>D;
vector<int> p={0};
v.pb(0);
for(int i=0;i<N;i++){
int a;cin>>a;
v.pb(a);
p.pb(a);
}
sort(p.begin(),p.end());
p.resize(unique(p.begin(),p.end())-p.begin());
map<int,int> l;
for(int i=0;i<p.size();i++)l[p[i]]=i;
for(int i=0;i<=N;i++)v[i]=l[v[i]];
for(int i=0;i<=N;i++){
if(i%dim==0)z.pb(fenwick());
}
for(int i=0;i<=N;i++){
q.insert(v[i],i);
q.pop(i-D);
val[i]=q.top();
}
int ans=0;
for(int i=1;i<=N;i++){
memo[i]=1;
int pos=i-i%dim;
for(int j=pos;j>=0;j-=dim){
if(ma[j/dim]<v[i]){
memo[i]=max(memo[i],z[j/dim].ma(v[i]-1)+1);
}else{
for(int h=min(i-1,j+dim-1);h>=j;h--){
if(val[h]>=v[i])break;
if(v[h]<v[i]){
memo[i]=max(memo[i],memo[h]+1);
}
}
break;
}
}
ans=max(ans,memo[i]);
ma[i/dim]=max(ma[i/dim],val[i]);
z[i/dim].upd(v[i],memo[i]);
}
cout<<ans<<endl;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:58:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(int i=0;i<p.size();i++)l[p[i]]=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... |