#include<bits/stdc++.h>
using namespace std;
const int nmax=2e5+42;
int n,x,inp[nmax];
int tree[4*nmax];
void update(int node,int l,int r,int pos,int val)
{
if(l==r)
{
tree[node]=val;
return;
}
int av=(l+r)/2;
if(pos<=av)update(node*2,l,av,pos,val);
else update(node*2+1,av+1,r,pos,val);
tree[node]=max(tree[node*2],tree[node*2+1]);
}
int query(int node,int l,int r,int lq,int rq)
{
if(l==lq&&r==rq)return tree[node];
int av=(l+r)/2,ret=0;
if(lq<=av)ret=max(ret,query(node*2,l,av,lq,min(av,rq)));
if(av<rq)ret=max(ret,query(node*2+1,av+1,r,max(av+1,lq),rq));
return ret;
}
int sorted_inp[nmax];
int longest_to_end[nmax];
int main()
{
scanf("%i%i",&n,&x);
for(int i=1;i<=n;i++)
{
scanf("%i",&inp[i]);
sorted_inp[i]=inp[i];
}
sort(sorted_inp+1,sorted_inp+n+1);
int output=0;
for(int i=n;i>=1;i--)
{
int pos_up=upper_bound(sorted_inp+1,sorted_inp+n+1,inp[i])-sorted_inp;
int pos_low=lower_bound(sorted_inp+1,sorted_inp+n+1,inp[i])-sorted_inp;
if(pos_up<=n)longest_to_end[i]=query(1,1,n,pos_up,n);
longest_to_end[i]++;
output=max(output,longest_to_end[i]);
update(1,1,n,pos_low,longest_to_end[i]);
//cout<<i<<" -> "<<longest_to_end[i]<<endl;
}
for(int i=0;i<4*nmax;i++)tree[i]=0;
for(int i=1;i<=n;i++)
{
//try to add
int pos_add=lower_bound(sorted_inp+1,sorted_inp+n+1,inp[i]+x)-sorted_inp;
pos_add--;
if(pos_add)
{
output=max(output,query(1,1,n,1,pos_add)+longest_to_end[i]);
//cout<<i<<" -> "<<query(1,1,n,1,pos_add)<<" "<<longest_to_end[i]<<endl;
}
int pos=lower_bound(sorted_inp+1,sorted_inp+n+1,inp[i])-sorted_inp;
pos--;
int current=0;
if(pos)current=query(1,1,n,1,pos);
current++;
pos++;
update(1,1,n,pos,current);
}
printf("%i\n",output);
return 0;
}
Compilation message
glo.cpp: In function 'int main()':
glo.cpp:39:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i%i",&n,&x);
~~~~~^~~~~~~~~~~~~~
glo.cpp:43:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%i",&inp[i]);
~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3456 KB |
Output is correct |
2 |
Correct |
6 ms |
3456 KB |
Output is correct |
3 |
Correct |
6 ms |
3456 KB |
Output is correct |
4 |
Correct |
7 ms |
3456 KB |
Output is correct |
5 |
Correct |
6 ms |
3456 KB |
Output is correct |
6 |
Correct |
6 ms |
3456 KB |
Output is correct |
7 |
Correct |
6 ms |
3456 KB |
Output is correct |
8 |
Correct |
7 ms |
3456 KB |
Output is correct |
9 |
Correct |
7 ms |
3456 KB |
Output is correct |
10 |
Correct |
7 ms |
3456 KB |
Output is correct |
11 |
Correct |
7 ms |
3456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3456 KB |
Output is correct |
2 |
Correct |
6 ms |
3456 KB |
Output is correct |
3 |
Correct |
6 ms |
3456 KB |
Output is correct |
4 |
Correct |
7 ms |
3456 KB |
Output is correct |
5 |
Correct |
6 ms |
3456 KB |
Output is correct |
6 |
Correct |
6 ms |
3456 KB |
Output is correct |
7 |
Correct |
6 ms |
3456 KB |
Output is correct |
8 |
Correct |
7 ms |
3456 KB |
Output is correct |
9 |
Correct |
7 ms |
3456 KB |
Output is correct |
10 |
Correct |
7 ms |
3456 KB |
Output is correct |
11 |
Correct |
7 ms |
3456 KB |
Output is correct |
12 |
Correct |
7 ms |
3456 KB |
Output is correct |
13 |
Correct |
7 ms |
3456 KB |
Output is correct |
14 |
Correct |
9 ms |
3456 KB |
Output is correct |
15 |
Correct |
7 ms |
3456 KB |
Output is correct |
16 |
Correct |
6 ms |
3456 KB |
Output is correct |
17 |
Correct |
6 ms |
3456 KB |
Output is correct |
18 |
Correct |
7 ms |
3456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3456 KB |
Output is correct |
2 |
Correct |
6 ms |
3456 KB |
Output is correct |
3 |
Correct |
6 ms |
3456 KB |
Output is correct |
4 |
Correct |
7 ms |
3456 KB |
Output is correct |
5 |
Correct |
6 ms |
3456 KB |
Output is correct |
6 |
Correct |
6 ms |
3456 KB |
Output is correct |
7 |
Correct |
6 ms |
3456 KB |
Output is correct |
8 |
Correct |
7 ms |
3456 KB |
Output is correct |
9 |
Correct |
7 ms |
3456 KB |
Output is correct |
10 |
Correct |
7 ms |
3456 KB |
Output is correct |
11 |
Correct |
7 ms |
3456 KB |
Output is correct |
12 |
Correct |
7 ms |
3456 KB |
Output is correct |
13 |
Correct |
7 ms |
3456 KB |
Output is correct |
14 |
Correct |
9 ms |
3456 KB |
Output is correct |
15 |
Correct |
7 ms |
3456 KB |
Output is correct |
16 |
Correct |
6 ms |
3456 KB |
Output is correct |
17 |
Correct |
6 ms |
3456 KB |
Output is correct |
18 |
Correct |
7 ms |
3456 KB |
Output is correct |
19 |
Correct |
7 ms |
3456 KB |
Output is correct |
20 |
Correct |
8 ms |
3456 KB |
Output is correct |
21 |
Correct |
7 ms |
3456 KB |
Output is correct |
22 |
Correct |
8 ms |
3456 KB |
Output is correct |
23 |
Correct |
7 ms |
3456 KB |
Output is correct |
24 |
Correct |
8 ms |
3456 KB |
Output is correct |
25 |
Correct |
7 ms |
3456 KB |
Output is correct |
26 |
Correct |
7 ms |
3456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
352 ms |
5752 KB |
Output is correct |
2 |
Correct |
371 ms |
7672 KB |
Output is correct |
3 |
Correct |
348 ms |
7820 KB |
Output is correct |
4 |
Correct |
370 ms |
7672 KB |
Output is correct |
5 |
Correct |
193 ms |
7032 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
4088 KB |
Output is correct |
2 |
Correct |
83 ms |
4472 KB |
Output is correct |
3 |
Correct |
84 ms |
4472 KB |
Output is correct |
4 |
Correct |
48 ms |
4344 KB |
Output is correct |
5 |
Correct |
6 ms |
3456 KB |
Output is correct |
6 |
Correct |
57 ms |
4352 KB |
Output is correct |
7 |
Correct |
72 ms |
4600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
148 ms |
4600 KB |
Output is correct |
2 |
Correct |
146 ms |
5664 KB |
Output is correct |
3 |
Correct |
307 ms |
7672 KB |
Output is correct |
4 |
Correct |
172 ms |
7032 KB |
Output is correct |
5 |
Correct |
99 ms |
5240 KB |
Output is correct |
6 |
Correct |
178 ms |
6904 KB |
Output is correct |
7 |
Correct |
178 ms |
7544 KB |
Output is correct |
8 |
Correct |
124 ms |
5624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
3456 KB |
Output is correct |
2 |
Correct |
6 ms |
3456 KB |
Output is correct |
3 |
Correct |
6 ms |
3456 KB |
Output is correct |
4 |
Correct |
7 ms |
3456 KB |
Output is correct |
5 |
Correct |
6 ms |
3456 KB |
Output is correct |
6 |
Correct |
6 ms |
3456 KB |
Output is correct |
7 |
Correct |
6 ms |
3456 KB |
Output is correct |
8 |
Correct |
7 ms |
3456 KB |
Output is correct |
9 |
Correct |
7 ms |
3456 KB |
Output is correct |
10 |
Correct |
7 ms |
3456 KB |
Output is correct |
11 |
Correct |
7 ms |
3456 KB |
Output is correct |
12 |
Correct |
7 ms |
3456 KB |
Output is correct |
13 |
Correct |
7 ms |
3456 KB |
Output is correct |
14 |
Correct |
9 ms |
3456 KB |
Output is correct |
15 |
Correct |
7 ms |
3456 KB |
Output is correct |
16 |
Correct |
6 ms |
3456 KB |
Output is correct |
17 |
Correct |
6 ms |
3456 KB |
Output is correct |
18 |
Correct |
7 ms |
3456 KB |
Output is correct |
19 |
Correct |
7 ms |
3456 KB |
Output is correct |
20 |
Correct |
8 ms |
3456 KB |
Output is correct |
21 |
Correct |
7 ms |
3456 KB |
Output is correct |
22 |
Correct |
8 ms |
3456 KB |
Output is correct |
23 |
Correct |
7 ms |
3456 KB |
Output is correct |
24 |
Correct |
8 ms |
3456 KB |
Output is correct |
25 |
Correct |
7 ms |
3456 KB |
Output is correct |
26 |
Correct |
7 ms |
3456 KB |
Output is correct |
27 |
Correct |
352 ms |
5752 KB |
Output is correct |
28 |
Correct |
371 ms |
7672 KB |
Output is correct |
29 |
Correct |
348 ms |
7820 KB |
Output is correct |
30 |
Correct |
370 ms |
7672 KB |
Output is correct |
31 |
Correct |
193 ms |
7032 KB |
Output is correct |
32 |
Correct |
83 ms |
4088 KB |
Output is correct |
33 |
Correct |
83 ms |
4472 KB |
Output is correct |
34 |
Correct |
84 ms |
4472 KB |
Output is correct |
35 |
Correct |
48 ms |
4344 KB |
Output is correct |
36 |
Correct |
6 ms |
3456 KB |
Output is correct |
37 |
Correct |
57 ms |
4352 KB |
Output is correct |
38 |
Correct |
72 ms |
4600 KB |
Output is correct |
39 |
Correct |
148 ms |
4600 KB |
Output is correct |
40 |
Correct |
146 ms |
5664 KB |
Output is correct |
41 |
Correct |
307 ms |
7672 KB |
Output is correct |
42 |
Correct |
172 ms |
7032 KB |
Output is correct |
43 |
Correct |
99 ms |
5240 KB |
Output is correct |
44 |
Correct |
178 ms |
6904 KB |
Output is correct |
45 |
Correct |
178 ms |
7544 KB |
Output is correct |
46 |
Correct |
124 ms |
5624 KB |
Output is correct |
47 |
Correct |
173 ms |
5624 KB |
Output is correct |
48 |
Correct |
172 ms |
5656 KB |
Output is correct |
49 |
Correct |
366 ms |
7892 KB |
Output is correct |
50 |
Correct |
173 ms |
7032 KB |
Output is correct |
51 |
Correct |
183 ms |
6136 KB |
Output is correct |
52 |
Correct |
247 ms |
7288 KB |
Output is correct |
53 |
Correct |
181 ms |
7008 KB |
Output is correct |
54 |
Correct |
177 ms |
7800 KB |
Output is correct |
55 |
Correct |
310 ms |
7816 KB |
Output is correct |