This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*input
7 1
100 600 600 200 300 500 500
*/
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("Ofast")
//order_of_key #of elements less than x
// find_by_order kth element
typedef long long int ll;
#define ld double
#define pii pair<ll,ll>
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (ll)x.size()
const ll maxn=3e5+5;
const ll maxlg=__lg(maxn)+2;
const ll INF64=4e17;
const int INF=0x3f3f3f3f;
const ll MOD=3006703054056749LL;
const ld PI=acos(-1);
const ld eps=1e-4;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
ll mult(ll a,ll b){
ll res=0;
while(b){
if(b&1) res+=a,res%=MOD;
a+=a;
a%=MOD;
b>>=1;
}
return res;
}
ll mypow(ll a,ll b){
ll res=1;
while(b){
if(b&1) res=mult(res,a);
a=mult(a,a);
b/=2;
}
return res;
}
int a[maxn];
struct seg{
int seg[4*maxn];
void upd(int idx,int l,int r,int p,int x){
if(r<p or l>p) return;
if(l==r){
seg[idx]=x;
return;
}
int mid=(l+r)/2;
upd(idx*2,l,mid,p,x);
upd(idx*2+1,mid+1,r,p,x);
seg[idx]=max(seg[idx*2],seg[idx*2+1]);
}
int query(int idx,int l,int r,int ql,int qr){
if(r<ql or l>qr) return 0;
if(ql<=l and r<=qr) return seg[idx];
int mid=(l+r)/2;
return max(query(idx*2,l,mid,ql,qr),query(idx*2+1,mid+1,r,ql,qr));
}
}dp;
struct seg2{
int seg[4*maxn];
void build(int idx,int l,int r){
if(l==r){
seg[idx]=INF;
return;
}
int mid=(l+r)/2;
build(idx*2,l,mid);
build(idx*2+1,mid+1,r);
seg[idx]=min(seg[idx*2],seg[idx*2+1]);
}
void upd(int idx,int l,int r,int p,int x){
if(r<p or l>p) return;
if(l==r){
seg[idx]=x;
return;
}
int mid=(l+r)/2;
upd(idx*2,l,mid,p,x);
upd(idx*2+1,mid+1,r,p,x);
seg[idx]=min(seg[idx*2],seg[idx*2+1]);
}
int query(int idx,int l,int r,int ql,int qr){
if(r<ql or l>qr) return INF;
if(ql<=l and r<=qr) return seg[idx];
int mid=(l+r)/2;
return min(query(idx*2,l,mid,ql,qr),query(idx*2+1,mid+1,r,ql,qr));
}
}segg;
int mn[maxn];
int32_t main(){
ios::sync_with_stdio(false),cin.tie(0);
int n,d;
cin>>n>>d;
vector<pii> v;
REP(i,n) cin>>a[i],v.pb({a[i],-i}),mn[i]=INF;
sort(ALL(v));
segg.build(1,0,n-1);
REP(asd,n){
int i=-v[asd].s;
mn[i]=i-d;
int p=i-d;
//cout<<mn[i]<<' ';
while(segg.query(1,0,n-1,p,i)<p){
//cout<<p<<' '<<i<<'\n';
p=segg.query(1,0,n-1,p,i);
}
mn[i]=p;
//cout<<mn[i]<<'\n';
segg.upd(1,0,n-1,i,mn[i]);
dp.upd(1,0,n-1,i,dp.query(1,0,n-1,max(0,mn[i]),i)+1);
}
//REP(i,n) cout<<mn[i]<<' ';
cout<<dp.query(1,0,n-1,0,n-1);
}
# | 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... |