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>
#define F first
#define S second
#define pb push_back
#define ppb pop_back
#define ep insert
#define endl '\n'
#define elif else if
#define pow pwr
#define sqrt sqrtt
//#define int long long
#define y1 YONE
typedef unsigned long long ull;
using namespace std;
const int N=3e5+5;
int a[N],n,d,bit[N],seg[4*N+5],b[N],dp[N];
void upd(int x){
while (x<=n){
bit[x]++;
x+=x&-x;
}return;
}int calc(int x){
int y=0;
while (x){
y+=bit[x];
x-=x&-x;
}return y;
}
int edit(int node,int l,int r,int idx,int val){
if (l==r) return seg[node]=val;
int mid=(l+r)/2;
if (idx<=mid) return seg[node]=max(edit(node*2,l,mid,idx,val),seg[node*2+1]);
return seg[node]=max(edit(node*2+1,mid+1,r,idx,val),seg[node*2]);
}
int query(int node,int l,int r,int lx,int rx){
if (r<lx || l>rx) return 0;
if (l>=lx && r<=rx) return seg[node];
int mid=(l+r)/2;
return max(query(node*2,l,mid,lx,rx),query(node*2+1,mid+1,r,lx,rx));
}
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
cin>>n>>d;
for (int i=1;i<=n;i++) cin>>a[i];
a[0]=INT_MIN;
if (d==1){
stack<int> s;
for (int i=1;i<=n;i++){
while (!s.empty()){
if (a[s.top()]<a[i]) s.pop();
else{
b[i]=s.top();
break;
}
}s.push(i);
}
int ans=0;
for (int i=n;i;i--){
upd(b[i]+1);
ans=max(ans,calc(i));
}cout<<ans<<endl;
return 0;
}
if (d==n){
map<int,int> mp;mp[INT_MIN]++;
for (int i=1;i<=n;i++) mp[a[i]]++;
int c=0;
for (auto u:mp) mp[u.F]=c++;
for (int i=1;i<=n;i++){
a[i]=mp[a[i]];
edit(1,1,n,a[i],query(1,1,n,0,a[i]-1)+1);
}cout<<query(1,1,n,0,4*N+4)<<endl;
return 0;
}int ans=0;
for (int i=1;i<=n;i++){
dp[i]=1;
int x=i;
for (int j=i-1;j;j--){
if (x-j>d) break;
if (a[j]>=a[i]) continue;
dp[i]=max(dp[i],dp[j]+1);
x=j;
}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... |