#include<bits/stdc++.h>
using namespace std ;
int mx = 7e5 ;
unordered_map<int,int> mp ;
vector<int> V ;
struct tree
{
vector<int> t ;
tree(int n)
{
t = vector<int> (4*n + 4 , 0) ;
}
void update(int node , int low , int high , int ind , int val)
{
if(low>ind || high<ind) return ;
if(low==high && low==ind)
{
t[node] = val ;
return ;
}
int mid = (low+high)>>1 , left = node<<1 , right = left|1 ;
update(left,low,mid,ind,val) , update(right,mid+1,high,ind,val) ;
t[node] = max(t[left],t[right]) ;
}
void clean(int node , int low , int high)
{
if(low==high)
{
t[node] = 0 ;
return ;
}
int mid = (low+high)>>1 , left = node<<1 , right = left|1 ;
clean(left,low,mid) , clean(right,mid+1,high) ;
t[node] = 0 ;
}
int query(int node , int low , int high , int l , int r)
{
if(l>r) return 0 ;
if(r<low || l>high) return 0 ;
if(l<=low && high<=r) return t[node] ;
int mid = (low+high)>>1 , left = node<<1 , right = left|1 ;
return max(query(left,low,mid,l,r) , query(right,mid+1,high,l,r)) ;
}
};
int main()
{
int n , x , ind = 0 ;
scanf("%d%d",&n,&x) ;
int a[n+1] , ans = 0 , dp[n+2][3] ;
for(int i = 1 ; i <= n ; ++i)
{
scanf("%d",&a[i]) ;
V.push_back(a[i]) ;
V.push_back(a[i] + x - 1) ;
V.push_back(a[i] - x + 1) ;
dp[i][1] = 0 , dp[i][2] = 0 ;
}
sort(V.begin(),V.end()) ;
for(int i : V)
{
if(mp[i]) continue ;
mp[i] = ++ind ;
}
++ind , ++ind ;
mx = ind ;
tree pref(mx+5) , suf(mx+5) ;
for(int i = 1 ; i <= n ; ++i)
{
dp[i][1] = pref.query(1,0,mx,1,mp[a[i] + x - 1]) ;
pref.update(1,0,mx,mp[a[i]],pref.query(1,0,mx,1,mp[a[i]] - 1) + 1) ;
}
for(int i = n ; i >= 1 ; --i)
{
dp[i][2] = suf.query(1,0,mx,mp[a[i]-x+1],mx) ;
suf.update(1,0,mx,mp[a[i]],suf.query(1,0,mx,mp[a[i]] + 1,mx) + 1) ;
}
pref.clean(1,0,mx) , suf.clean(1,0,mx) ;
for(int i = 1 ; i <= n ; ++i)
{
ans = max(ans,dp[i][2] + pref.query(1,0,mx,1,mp[a[i]] - 1) + 1) ;
pref.update(1,0,mx,mp[a[i]],pref.query(1,0,mx,1,mp[a[i]] - 1) + 1) ;
}
for(int i = n ; i >= 1 ; --i)
{
ans = max(ans,dp[i][1] + suf.query(1,0,mx,mp[a[i]] + 1,mx) + 1) ;
suf.update(1,0,mx,mp[a[i]],suf.query(1,0,mx,mp[a[i]] + 1,mx) + 1) ;
}
printf("%d\n",ans) ;
return 0 ;
}
Compilation message
glo.cpp: In function 'int main()':
glo.cpp:57:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
57 | scanf("%d%d",&n,&x) ;
| ~~~~~^~~~~~~~~~~~~~
glo.cpp:61:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
61 | scanf("%d",&a[i]) ;
| ~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 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 |
0 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 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 |
0 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 |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
0 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 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 |
0 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 |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
0 ms |
364 KB |
Output is correct |
19 |
Correct |
3 ms |
620 KB |
Output is correct |
20 |
Correct |
3 ms |
620 KB |
Output is correct |
21 |
Correct |
5 ms |
620 KB |
Output is correct |
22 |
Correct |
3 ms |
620 KB |
Output is correct |
23 |
Correct |
2 ms |
492 KB |
Output is correct |
24 |
Correct |
2 ms |
492 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 |
1431 ms |
51800 KB |
Output is correct |
2 |
Correct |
1477 ms |
51948 KB |
Output is correct |
3 |
Correct |
1483 ms |
51832 KB |
Output is correct |
4 |
Correct |
1475 ms |
51832 KB |
Output is correct |
5 |
Correct |
646 ms |
29524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
312 ms |
13152 KB |
Output is correct |
2 |
Correct |
266 ms |
13152 KB |
Output is correct |
3 |
Correct |
280 ms |
13188 KB |
Output is correct |
4 |
Correct |
137 ms |
7392 KB |
Output is correct |
5 |
Correct |
0 ms |
364 KB |
Output is correct |
6 |
Correct |
108 ms |
3552 KB |
Output is correct |
7 |
Correct |
182 ms |
9952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
695 ms |
25992 KB |
Output is correct |
2 |
Correct |
718 ms |
25992 KB |
Output is correct |
3 |
Correct |
1640 ms |
51836 KB |
Output is correct |
4 |
Correct |
750 ms |
30548 KB |
Output is correct |
5 |
Correct |
329 ms |
26760 KB |
Output is correct |
6 |
Correct |
637 ms |
50796 KB |
Output is correct |
7 |
Correct |
635 ms |
51436 KB |
Output is correct |
8 |
Correct |
583 ms |
26888 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
0 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
0 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
0 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 |
0 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 |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
0 ms |
364 KB |
Output is correct |
19 |
Correct |
3 ms |
620 KB |
Output is correct |
20 |
Correct |
3 ms |
620 KB |
Output is correct |
21 |
Correct |
5 ms |
620 KB |
Output is correct |
22 |
Correct |
3 ms |
620 KB |
Output is correct |
23 |
Correct |
2 ms |
492 KB |
Output is correct |
24 |
Correct |
2 ms |
492 KB |
Output is correct |
25 |
Correct |
2 ms |
492 KB |
Output is correct |
26 |
Correct |
2 ms |
492 KB |
Output is correct |
27 |
Correct |
1431 ms |
51800 KB |
Output is correct |
28 |
Correct |
1477 ms |
51948 KB |
Output is correct |
29 |
Correct |
1483 ms |
51832 KB |
Output is correct |
30 |
Correct |
1475 ms |
51832 KB |
Output is correct |
31 |
Correct |
646 ms |
29524 KB |
Output is correct |
32 |
Correct |
312 ms |
13152 KB |
Output is correct |
33 |
Correct |
266 ms |
13152 KB |
Output is correct |
34 |
Correct |
280 ms |
13188 KB |
Output is correct |
35 |
Correct |
137 ms |
7392 KB |
Output is correct |
36 |
Correct |
0 ms |
364 KB |
Output is correct |
37 |
Correct |
108 ms |
3552 KB |
Output is correct |
38 |
Correct |
182 ms |
9952 KB |
Output is correct |
39 |
Correct |
695 ms |
25992 KB |
Output is correct |
40 |
Correct |
718 ms |
25992 KB |
Output is correct |
41 |
Correct |
1640 ms |
51836 KB |
Output is correct |
42 |
Correct |
750 ms |
30548 KB |
Output is correct |
43 |
Correct |
329 ms |
26760 KB |
Output is correct |
44 |
Correct |
637 ms |
50796 KB |
Output is correct |
45 |
Correct |
635 ms |
51436 KB |
Output is correct |
46 |
Correct |
583 ms |
26888 KB |
Output is correct |
47 |
Correct |
712 ms |
27016 KB |
Output is correct |
48 |
Correct |
751 ms |
27016 KB |
Output is correct |
49 |
Correct |
1571 ms |
53644 KB |
Output is correct |
50 |
Correct |
735 ms |
30720 KB |
Output is correct |
51 |
Correct |
399 ms |
19932 KB |
Output is correct |
52 |
Correct |
537 ms |
21844 KB |
Output is correct |
53 |
Correct |
653 ms |
52972 KB |
Output is correct |
54 |
Correct |
659 ms |
53740 KB |
Output is correct |
55 |
Correct |
1196 ms |
41300 KB |
Output is correct |