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 MAXN 2000007
using namespace std;
int a[MAXN],v[MAXN],seg[2][4*MAXN],dp[2][MAXN];
map<int,int> m;
void upd(int k,int l,int r,int val,int pos,int ind)
{
if(l==r) {seg[k][ind]=val; return;}
int s=(l+r)/2;
if(pos<=s) upd(k,l,s,val,pos,2*ind);
else upd(k,s+1,r,val,pos,2*ind+1);
seg[k][ind]=max(seg[k][2*ind],seg[k][2*ind+1]);
}
int nmax(int k,int l,int r,int lt,int rt,int ind)
{
if(l>rt || r<lt) return 0;
if(l>=lt && r<=rt) return seg[k][ind];
int s=(l+r)/2;
return max(nmax(k,l,s,lt,rt,2*ind),nmax(k,s+1,r,lt,rt,2*ind+1));
}
int binarna(int l,int r,int val)
{
if(l==r) return l+1;
int s=(l+r+1)/2;
if(v[s]>=val) return binarna(l,s-1,val);
return binarna(s,r,val);
}
int main()
{
int n,x;
scanf("%d%d",&n,&x);
for(int i=0;i<n;i++) scanf("%d",&a[i]);
for(int i=0;i<n;i++) v[i]=a[i];
sort(v,v+n);
for(int i=0;i<n;i++) m[v[i]]=i+1;
for(int i=0;i<n;i++)
{
dp[1][i]=max(nmax(1,0,n,0,m[a[i]]-1,1),nmax(0,0,n,0,binarna(0,n-1,a[i]+x),1))+1;
dp[0][i]=nmax(0,0,n,0,m[a[i]]-1,1)+1;
upd(0,0,n,dp[0][i],m[a[i]],1);
upd(1,0,n,dp[1][i],m[a[i]],1);
if(i>0) dp[1][i]=max(dp[1][i],dp[1][i-1]);
}
printf("%d",dp[1][n-1]);
return 0;
}
Compilation message (stderr)
glo.cpp: In function 'int main()':
glo.cpp:31:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d",&n,&x);
~~~~~^~~~~~~~~~~~~~
glo.cpp:32:31: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=0;i<n;i++) scanf("%d",&a[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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |