#include<bits/stdc++.h>
#define f first
#define s second
#define int long long
using namespace std;
const int N=2e5+5,mod=1e9+7;
int t,tree[4*N],R[N],L[N],a[N],b[N],n,x;
map<int,int> ind;
int getans(int u,int start,int end,int l,int r) {
if(l>end || r<start) return 0 ;
if(start<=l && r<=end) return tree[u];
int mid = (l+r)/2;
return max(getans(2*u,start,end,l,mid),getans(2*u+1,start,end,mid+1,r));
}
void update(int u,int ind,int l,int r,int val) {
if(l>ind || r<ind) return;
if(l==r) {
tree[u] = val;
return;
}
int mid = (l+r)/2;
update(2*u,ind,l,mid,val);
update(2*u+1,ind,mid+1,r,val);
tree[u] = max(tree[2*u],tree[2*u+1]);
}
main(){
cin>>n>>x;
for(int i=1;i<=n;i++){
cin >> a[i];
b[i] = a[i];
}
sort(b+1,b+n+1);
int idx = 0;
for(int i = 1; i<=n; i++) {
if(b[i] != b[i-1]) idx++;
ind[b[i]] = idx;
}
for(int i=n;i>=1;i--) {
R[i] = getans(1,ind[a[i]]+1,n,1,n) + 1;
update(1,ind[a[i]],1,n,R[i]);
}
int ans = 0;
for(int i=1;i<=4*n;i++) tree[i] = 0;
for(int i=1;i<=n;i++) {
L[i] = getans(1,1,ind[a[i]]-1,1,n) + 1;
int l = 1, r = n,c = 0;
while(l<=r) {
int mid = (l+r)/2;
if(b[mid] < a[i] + x) {
c = mid;
l = mid + 1;
}
else r = mid - 1;
}
c = ind[b[c]];
ans = max(ans, getans(1,1,c,1,n) + R[i]);
update(1,ind[a[i]],1,n,L[i]);
}
for(int i=1;i<=4*n;i++) tree[i] = 0;
ind[0] = n + 1;
for(int i = n; i>=1; i--) {
int l = 1, r = n, c= 0;
while(l<=r) {
int mid = (l+r)/2;
if(b[mid] > a[i] - x) {
r = mid - 1;
c = mid;
}
else l = mid + 1;
}
c = ind[b[c]];
ans = max(ans,getans(1,c,n,1,n) + L[i]);
update(1,ind[a[i]],1,n,R[i]);
}
cout<<ans;
}
Compilation message
glo.cpp:26:2: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
26 | main(){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
3 ms |
460 KB |
Output is correct |
20 |
Correct |
3 ms |
436 KB |
Output is correct |
21 |
Correct |
3 ms |
420 KB |
Output is correct |
22 |
Correct |
3 ms |
440 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
424 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1423 ms |
25280 KB |
Output is correct |
2 |
Correct |
1475 ms |
27228 KB |
Output is correct |
3 |
Correct |
1532 ms |
27364 KB |
Output is correct |
4 |
Correct |
1460 ms |
27244 KB |
Output is correct |
5 |
Correct |
522 ms |
20212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
223 ms |
6664 KB |
Output is correct |
2 |
Correct |
206 ms |
6468 KB |
Output is correct |
3 |
Correct |
206 ms |
6552 KB |
Output is correct |
4 |
Correct |
107 ms |
4964 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
121 ms |
4932 KB |
Output is correct |
7 |
Correct |
171 ms |
6568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
483 ms |
12752 KB |
Output is correct |
2 |
Correct |
507 ms |
12736 KB |
Output is correct |
3 |
Correct |
1211 ms |
25212 KB |
Output is correct |
4 |
Correct |
520 ms |
19028 KB |
Output is correct |
5 |
Correct |
292 ms |
12856 KB |
Output is correct |
6 |
Correct |
481 ms |
24092 KB |
Output is correct |
7 |
Correct |
496 ms |
24004 KB |
Output is correct |
8 |
Correct |
328 ms |
12740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
0 ms |
332 KB |
Output is correct |
10 |
Correct |
0 ms |
332 KB |
Output is correct |
11 |
Correct |
0 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
3 ms |
460 KB |
Output is correct |
20 |
Correct |
3 ms |
436 KB |
Output is correct |
21 |
Correct |
3 ms |
420 KB |
Output is correct |
22 |
Correct |
3 ms |
440 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
424 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
2 ms |
332 KB |
Output is correct |
27 |
Correct |
1423 ms |
25280 KB |
Output is correct |
28 |
Correct |
1475 ms |
27228 KB |
Output is correct |
29 |
Correct |
1532 ms |
27364 KB |
Output is correct |
30 |
Correct |
1460 ms |
27244 KB |
Output is correct |
31 |
Correct |
522 ms |
20212 KB |
Output is correct |
32 |
Correct |
223 ms |
6664 KB |
Output is correct |
33 |
Correct |
206 ms |
6468 KB |
Output is correct |
34 |
Correct |
206 ms |
6552 KB |
Output is correct |
35 |
Correct |
107 ms |
4964 KB |
Output is correct |
36 |
Correct |
0 ms |
204 KB |
Output is correct |
37 |
Correct |
121 ms |
4932 KB |
Output is correct |
38 |
Correct |
171 ms |
6568 KB |
Output is correct |
39 |
Correct |
483 ms |
12752 KB |
Output is correct |
40 |
Correct |
507 ms |
12736 KB |
Output is correct |
41 |
Correct |
1211 ms |
25212 KB |
Output is correct |
42 |
Correct |
520 ms |
19028 KB |
Output is correct |
43 |
Correct |
292 ms |
12856 KB |
Output is correct |
44 |
Correct |
481 ms |
24092 KB |
Output is correct |
45 |
Correct |
496 ms |
24004 KB |
Output is correct |
46 |
Correct |
328 ms |
12740 KB |
Output is correct |
47 |
Correct |
558 ms |
13668 KB |
Output is correct |
48 |
Correct |
567 ms |
13800 KB |
Output is correct |
49 |
Correct |
1445 ms |
27248 KB |
Output is correct |
50 |
Correct |
474 ms |
20228 KB |
Output is correct |
51 |
Correct |
398 ms |
16708 KB |
Output is correct |
52 |
Correct |
628 ms |
26580 KB |
Output is correct |
53 |
Correct |
507 ms |
26692 KB |
Output is correct |
54 |
Correct |
521 ms |
27172 KB |
Output is correct |
55 |
Correct |
903 ms |
27276 KB |
Output is correct |