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 getbit(i, j) ((i >> j) & 1)
#define maxn 300005
#define name "main"
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
long long GetRandom(long long l, long long r)
{
return uniform_int_distribution<long long> (l, r)(rng);
}
int n,d,a[maxn],st[maxn*4],dp[maxn],dem[maxn],sl,res;
pair<int,int> b[maxn];
void update(int id,int l,int r,int vt,int val)
{
if(l>vt||r<vt) return;
if(l==r)
{
st[id]=val;
return;
}
int mid=(l+r)/2;
update(id*2,l,mid,vt,val);
update(id*2+1,mid+1,r,vt,val);
st[id]=max(st[id*2],st[id*2+1]);
}
int get(int id,int l,int r,int d,int c)
{
if(l>c||r<d) return 0;
if(l>=d&&r<=c) return st[id];
int mid=(l+r)/2;
return max(get(id*2,l,mid,d,c),get(id*2+1,mid+1,r,d,c));
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
if(fopen(name".inp","r"))
{
freopen(name".inp","r",stdin);
freopen(name".out","w",stdout);
}
cin >> n >> d;
for(int i=1;i<=n;i++)
{
cin >> a[i];
b[i]={a[i],i};
}
sort(b+1,b+n+1);
sl=1;
a[b[1].second]=sl;
for(int i=2;i<=n;i++)
{
if(b[i].first>b[i-1].first) sl++;
a[b[i].second]=sl;
}
int vt=1;
for(int i=1;i<=n;i++)
{
while(vt<i-d)
{
dem[a[vt]]--;
if(dem[a[vt]]==0) update(1,1,sl,a[vt],0);
vt++;
}
dp[i]=get(1,1,sl,1,a[i]-1)+1;
dem[a[i]]++;
res=max(res,dp[i]);
update(1,1,sl,a[i],dp[i]);
}
cout<<res;
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:46:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | freopen(name".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
Main.cpp:47:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | freopen(name".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | 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... |