# include <bits/stdc++.h>
using namespace std;
map <long long,long long> mp;
const long long maxn = 200005;
long long heights[maxn],k[maxn];
long long a[maxn];
long long dpleft[maxn];
long long dpright[maxn];
struct segment
{
long long segm[2000005];
void update(long long v, long long from, long long to, long long x, long long delta)
{ // cout<<" ** "<<v<<" "<<from<<" "<<to<<" "<<x<<" "<<delta<<" "<<segm[v]<<endl;
if(from==to)
{
segm[v] = delta;
return ;
}
long long mid = (from+to)>>1;
if(x<=mid)update(2*v,from,mid,x,delta);
else
update(2*v+1,mid+1,to,x,delta);
segm[v] = max(segm[2*v],segm[2*v+1]);
}
long long query(long long v, long long from, long long to, long long l, long long r)
{
// cout<<" "<<v<<" "<<from<<" "<<to<<" "<<l<<" "<<r<<" "<<segm[v]<<endl;
if(r<l)return 0;
if(l<=from&&to<=r)return segm[v];
long long mid = (from+to)>>1;
long long ans = 0;
if(l<=mid)ans = query(2*v,from,mid,l,r);
if(r>mid)ans = max(ans,query(2*v+1,mid+1,to,l,r));
return ans;
}
};
segment l,r,f;
long long findidx(long long x, long long n)
{
long long uk1 = 1;
long long uk2 = n;
long long mid;
if(k[uk1]>=x)return 0;
while(uk1<uk2)
{
mid = (uk1+uk2+1)>>1;
if(k[mid]>=x)uk2 = mid-1;
else
uk1 = mid;
}
return uk2;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
long long n,x;
cin>>n>>x;
long long i,j;
for(i=1;i<=n;i++)
{
cin>>a[i];
heights[i] = a[i];
}
sort(heights+1,heights+n+1);
long long num = 0;
for(i=1;i<=n;i++)
{
if(heights[i]!=heights[i-1])
{
num++;
mp[heights[i]] = num;
k[num] = heights[i];
}
}
for(i=1;i<=n;i++)
{
// cout<<"SEARCH"<<mp[a[i]]-1<<endl;
dpleft[i] = 1+l.query(1,1,num,1,mp[a[i]]-1);
l.update(1,1,num,mp[a[i]],dpleft[i]);
// cout<<i<<" "<<dpleft[i]<<endl;
}
// cout<<endl;
for(i=n;i>0;i--)
{
dpright[i] = 1+r.query(1,1,num,mp[a[i]]+1,num);
r.update(1,1,num,mp[a[i]],dpright[i]);
// cout<<i<<" "<<dpright[i]<<endl;
}
// cout<<endl;
long long maxans = 0;
for(i=1;i<=n;i++)
{
long long p = findidx(a[i]+x,num);
maxans = max(dpright[i]+f.query(1,1,num,1,p),maxans);// cout<<i<<" "<<p<<" "<<maxans<<" "<<mp[a[i]]<<endl;
f.update(1,1,num,mp[a[i]],dpleft[i]);
}
maxans = max(maxans,f.query(1,1,num,1,num));
cout<<maxans<<endl;
}
Compilation message
glo.cpp: In function 'int main()':
glo.cpp:60:17: warning: unused variable 'j' [-Wunused-variable]
60 | long long i,j;
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
2 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
2 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
3 ms |
492 KB |
Output is correct |
20 |
Correct |
3 ms |
492 KB |
Output is correct |
21 |
Correct |
3 ms |
492 KB |
Output is correct |
22 |
Correct |
3 ms |
492 KB |
Output is correct |
23 |
Correct |
2 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
504 KB |
Output is correct |
25 |
Correct |
2 ms |
492 KB |
Output is correct |
26 |
Correct |
2 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1079 ms |
33108 KB |
Output is correct |
2 |
Correct |
1113 ms |
33132 KB |
Output is correct |
3 |
Correct |
1171 ms |
33172 KB |
Output is correct |
4 |
Correct |
1145 ms |
33100 KB |
Output is correct |
5 |
Correct |
289 ms |
19820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
166 ms |
8608 KB |
Output is correct |
2 |
Correct |
191 ms |
8556 KB |
Output is correct |
3 |
Correct |
152 ms |
8684 KB |
Output is correct |
4 |
Correct |
62 ms |
5228 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
64 ms |
5296 KB |
Output is correct |
7 |
Correct |
107 ms |
8684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
369 ms |
16748 KB |
Output is correct |
2 |
Correct |
366 ms |
16876 KB |
Output is correct |
3 |
Correct |
996 ms |
33132 KB |
Output is correct |
4 |
Correct |
258 ms |
20076 KB |
Output is correct |
5 |
Correct |
159 ms |
16748 KB |
Output is correct |
6 |
Correct |
303 ms |
32076 KB |
Output is correct |
7 |
Correct |
303 ms |
31980 KB |
Output is correct |
8 |
Correct |
220 ms |
16876 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
1 ms |
364 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
2 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
3 ms |
492 KB |
Output is correct |
20 |
Correct |
3 ms |
492 KB |
Output is correct |
21 |
Correct |
3 ms |
492 KB |
Output is correct |
22 |
Correct |
3 ms |
492 KB |
Output is correct |
23 |
Correct |
2 ms |
512 KB |
Output is correct |
24 |
Correct |
2 ms |
504 KB |
Output is correct |
25 |
Correct |
2 ms |
492 KB |
Output is correct |
26 |
Correct |
2 ms |
492 KB |
Output is correct |
27 |
Correct |
1079 ms |
33108 KB |
Output is correct |
28 |
Correct |
1113 ms |
33132 KB |
Output is correct |
29 |
Correct |
1171 ms |
33172 KB |
Output is correct |
30 |
Correct |
1145 ms |
33100 KB |
Output is correct |
31 |
Correct |
289 ms |
19820 KB |
Output is correct |
32 |
Correct |
166 ms |
8608 KB |
Output is correct |
33 |
Correct |
191 ms |
8556 KB |
Output is correct |
34 |
Correct |
152 ms |
8684 KB |
Output is correct |
35 |
Correct |
62 ms |
5228 KB |
Output is correct |
36 |
Correct |
1 ms |
364 KB |
Output is correct |
37 |
Correct |
64 ms |
5296 KB |
Output is correct |
38 |
Correct |
107 ms |
8684 KB |
Output is correct |
39 |
Correct |
369 ms |
16748 KB |
Output is correct |
40 |
Correct |
366 ms |
16876 KB |
Output is correct |
41 |
Correct |
996 ms |
33132 KB |
Output is correct |
42 |
Correct |
258 ms |
20076 KB |
Output is correct |
43 |
Correct |
159 ms |
16748 KB |
Output is correct |
44 |
Correct |
303 ms |
32076 KB |
Output is correct |
45 |
Correct |
303 ms |
31980 KB |
Output is correct |
46 |
Correct |
220 ms |
16876 KB |
Output is correct |
47 |
Correct |
473 ms |
17772 KB |
Output is correct |
48 |
Correct |
412 ms |
17644 KB |
Output is correct |
49 |
Correct |
1112 ms |
34984 KB |
Output is correct |
50 |
Correct |
291 ms |
21100 KB |
Output is correct |
51 |
Correct |
243 ms |
19180 KB |
Output is correct |
52 |
Correct |
393 ms |
34284 KB |
Output is correct |
53 |
Correct |
324 ms |
34432 KB |
Output is correct |
54 |
Correct |
325 ms |
35052 KB |
Output is correct |
55 |
Correct |
606 ms |
35180 KB |
Output is correct |