#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=2e5+10;
const ll inf=0;
const ll mod=1e9+7;
ll n,a[maxN];
ll st[4*maxN];
void build()
{
fill(st,st+4*maxN,-inf);
}
void update(ll pos,ll val,ll id=1,ll l=1,ll r=n)
{
if(l==r)
{
st[id]=max(st[id],val);
return;
}
ll mid=l+r>>1;
if(pos<=mid) update(pos,val,id*2,l,mid);
else update(pos,val,id*2+1,mid+1,r);
st[id]=max(st[id*2],st[id*2+1]);
}
ll get(ll i,ll j,ll id=1,ll l=1,ll r=n)
{
if(j<l||r<i) return -inf;
if(i<=l&&r<=j) return st[id];
ll mid=l+r>>1;
return max(get(i,j,id*2,l,mid),get(i,j,id*2+1,mid+1,r));
}
ll x,dp1[maxN],dp2[maxN];
vector<ll>vec;
void solve()
{
cin >> n >> x;
vec.pb(-1);
for(int i=1;i<=n;i++) cin >>a[i],vec.pb(a[i]);
sort(vec.begin(),vec.end());
for(int i=1;i<=n;i++)
{
ll p=lower_bound(vec.begin(),vec.end(),a[i])-vec.begin()-1;
dp1[i]=max(1ll,get(1,p)+1);
p++;
update(p,dp1[i]);
}
build();
for(int i=n;i>=1;i--)
{
ll p=upper_bound(vec.begin(),vec.end(),a[i])-vec.begin();
dp2[i]=max(1ll,get(p,n)+1);
p--;
update(p,dp2[i]);
}
build();
ll ans=dp1[n];
for(int i=1;i<=n;i++)
{
ll p=lower_bound(vec.begin(),vec.end(),a[i]+x)-vec.begin()-1;
ans=max(ans,dp2[i]+get(1,p));
p=lower_bound(vec.begin(),vec.end(),a[i])-vec.begin();
update(p,dp1[i]);
}
build();
for(int i=n;i>=1;i--)
{
ll p=upper_bound(vec.begin(),vec.end(),a[i]-x)-vec.begin();
ans=max(ans,dp1[i]+get(p,n));
p=lower_bound(vec.begin(),vec.end(),a[i])-vec.begin();
update(p,dp2[i]);
}
cout << ans;
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message
glo.cpp: In function 'void update(ll, ll, ll, ll, ll)':
glo.cpp:26:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
26 | ll mid=l+r>>1;
| ~^~
glo.cpp: In function 'll get(ll, ll, ll, ll, ll)':
glo.cpp:35:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
35 | ll mid=l+r>>1;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6484 KB |
Output is correct |
3 |
Correct |
3 ms |
6484 KB |
Output is correct |
4 |
Correct |
4 ms |
6612 KB |
Output is correct |
5 |
Correct |
3 ms |
6484 KB |
Output is correct |
6 |
Correct |
3 ms |
6484 KB |
Output is correct |
7 |
Correct |
4 ms |
6484 KB |
Output is correct |
8 |
Correct |
4 ms |
6612 KB |
Output is correct |
9 |
Correct |
4 ms |
6604 KB |
Output is correct |
10 |
Correct |
4 ms |
6484 KB |
Output is correct |
11 |
Correct |
5 ms |
6612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6484 KB |
Output is correct |
3 |
Correct |
3 ms |
6484 KB |
Output is correct |
4 |
Correct |
4 ms |
6612 KB |
Output is correct |
5 |
Correct |
3 ms |
6484 KB |
Output is correct |
6 |
Correct |
3 ms |
6484 KB |
Output is correct |
7 |
Correct |
4 ms |
6484 KB |
Output is correct |
8 |
Correct |
4 ms |
6612 KB |
Output is correct |
9 |
Correct |
4 ms |
6604 KB |
Output is correct |
10 |
Correct |
4 ms |
6484 KB |
Output is correct |
11 |
Correct |
5 ms |
6612 KB |
Output is correct |
12 |
Correct |
4 ms |
6612 KB |
Output is correct |
13 |
Correct |
3 ms |
6484 KB |
Output is correct |
14 |
Correct |
3 ms |
6612 KB |
Output is correct |
15 |
Correct |
4 ms |
6612 KB |
Output is correct |
16 |
Correct |
4 ms |
6612 KB |
Output is correct |
17 |
Correct |
4 ms |
6484 KB |
Output is correct |
18 |
Correct |
4 ms |
6612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6484 KB |
Output is correct |
3 |
Correct |
3 ms |
6484 KB |
Output is correct |
4 |
Correct |
4 ms |
6612 KB |
Output is correct |
5 |
Correct |
3 ms |
6484 KB |
Output is correct |
6 |
Correct |
3 ms |
6484 KB |
Output is correct |
7 |
Correct |
4 ms |
6484 KB |
Output is correct |
8 |
Correct |
4 ms |
6612 KB |
Output is correct |
9 |
Correct |
4 ms |
6604 KB |
Output is correct |
10 |
Correct |
4 ms |
6484 KB |
Output is correct |
11 |
Correct |
5 ms |
6612 KB |
Output is correct |
12 |
Correct |
4 ms |
6612 KB |
Output is correct |
13 |
Correct |
3 ms |
6484 KB |
Output is correct |
14 |
Correct |
3 ms |
6612 KB |
Output is correct |
15 |
Correct |
4 ms |
6612 KB |
Output is correct |
16 |
Correct |
4 ms |
6612 KB |
Output is correct |
17 |
Correct |
4 ms |
6484 KB |
Output is correct |
18 |
Correct |
4 ms |
6612 KB |
Output is correct |
19 |
Correct |
5 ms |
6612 KB |
Output is correct |
20 |
Correct |
6 ms |
6612 KB |
Output is correct |
21 |
Correct |
5 ms |
6612 KB |
Output is correct |
22 |
Correct |
6 ms |
6608 KB |
Output is correct |
23 |
Correct |
5 ms |
6604 KB |
Output is correct |
24 |
Correct |
4 ms |
6612 KB |
Output is correct |
25 |
Correct |
5 ms |
6608 KB |
Output is correct |
26 |
Correct |
5 ms |
6612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
554 ms |
14808 KB |
Output is correct |
2 |
Correct |
485 ms |
14820 KB |
Output is correct |
3 |
Correct |
518 ms |
14812 KB |
Output is correct |
4 |
Correct |
516 ms |
14784 KB |
Output is correct |
5 |
Correct |
221 ms |
14040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
8656 KB |
Output is correct |
2 |
Correct |
110 ms |
8652 KB |
Output is correct |
3 |
Correct |
103 ms |
8776 KB |
Output is correct |
4 |
Correct |
50 ms |
8488 KB |
Output is correct |
5 |
Correct |
4 ms |
6484 KB |
Output is correct |
6 |
Correct |
67 ms |
8596 KB |
Output is correct |
7 |
Correct |
103 ms |
8732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
181 ms |
10820 KB |
Output is correct |
2 |
Correct |
162 ms |
10716 KB |
Output is correct |
3 |
Correct |
464 ms |
14860 KB |
Output is correct |
4 |
Correct |
172 ms |
14084 KB |
Output is correct |
5 |
Correct |
143 ms |
10368 KB |
Output is correct |
6 |
Correct |
183 ms |
13816 KB |
Output is correct |
7 |
Correct |
189 ms |
14548 KB |
Output is correct |
8 |
Correct |
143 ms |
10740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6484 KB |
Output is correct |
3 |
Correct |
3 ms |
6484 KB |
Output is correct |
4 |
Correct |
4 ms |
6612 KB |
Output is correct |
5 |
Correct |
3 ms |
6484 KB |
Output is correct |
6 |
Correct |
3 ms |
6484 KB |
Output is correct |
7 |
Correct |
4 ms |
6484 KB |
Output is correct |
8 |
Correct |
4 ms |
6612 KB |
Output is correct |
9 |
Correct |
4 ms |
6604 KB |
Output is correct |
10 |
Correct |
4 ms |
6484 KB |
Output is correct |
11 |
Correct |
5 ms |
6612 KB |
Output is correct |
12 |
Correct |
4 ms |
6612 KB |
Output is correct |
13 |
Correct |
3 ms |
6484 KB |
Output is correct |
14 |
Correct |
3 ms |
6612 KB |
Output is correct |
15 |
Correct |
4 ms |
6612 KB |
Output is correct |
16 |
Correct |
4 ms |
6612 KB |
Output is correct |
17 |
Correct |
4 ms |
6484 KB |
Output is correct |
18 |
Correct |
4 ms |
6612 KB |
Output is correct |
19 |
Correct |
5 ms |
6612 KB |
Output is correct |
20 |
Correct |
6 ms |
6612 KB |
Output is correct |
21 |
Correct |
5 ms |
6612 KB |
Output is correct |
22 |
Correct |
6 ms |
6608 KB |
Output is correct |
23 |
Correct |
5 ms |
6604 KB |
Output is correct |
24 |
Correct |
4 ms |
6612 KB |
Output is correct |
25 |
Correct |
5 ms |
6608 KB |
Output is correct |
26 |
Correct |
5 ms |
6612 KB |
Output is correct |
27 |
Correct |
554 ms |
14808 KB |
Output is correct |
28 |
Correct |
485 ms |
14820 KB |
Output is correct |
29 |
Correct |
518 ms |
14812 KB |
Output is correct |
30 |
Correct |
516 ms |
14784 KB |
Output is correct |
31 |
Correct |
221 ms |
14040 KB |
Output is correct |
32 |
Correct |
108 ms |
8656 KB |
Output is correct |
33 |
Correct |
110 ms |
8652 KB |
Output is correct |
34 |
Correct |
103 ms |
8776 KB |
Output is correct |
35 |
Correct |
50 ms |
8488 KB |
Output is correct |
36 |
Correct |
4 ms |
6484 KB |
Output is correct |
37 |
Correct |
67 ms |
8596 KB |
Output is correct |
38 |
Correct |
103 ms |
8732 KB |
Output is correct |
39 |
Correct |
181 ms |
10820 KB |
Output is correct |
40 |
Correct |
162 ms |
10716 KB |
Output is correct |
41 |
Correct |
464 ms |
14860 KB |
Output is correct |
42 |
Correct |
172 ms |
14084 KB |
Output is correct |
43 |
Correct |
143 ms |
10368 KB |
Output is correct |
44 |
Correct |
183 ms |
13816 KB |
Output is correct |
45 |
Correct |
189 ms |
14548 KB |
Output is correct |
46 |
Correct |
143 ms |
10740 KB |
Output is correct |
47 |
Correct |
223 ms |
10700 KB |
Output is correct |
48 |
Correct |
230 ms |
10720 KB |
Output is correct |
49 |
Correct |
488 ms |
14808 KB |
Output is correct |
50 |
Correct |
208 ms |
14044 KB |
Output is correct |
51 |
Correct |
184 ms |
12352 KB |
Output is correct |
52 |
Correct |
280 ms |
14144 KB |
Output is correct |
53 |
Correct |
202 ms |
14112 KB |
Output is correct |
54 |
Correct |
195 ms |
14936 KB |
Output is correct |
55 |
Correct |
375 ms |
14920 KB |
Output is correct |