#include <bits/stdc++.h>
using namespace std;
int n,d,i,k,N=1,ans;
int mas[200001],num[200001],tree[530000],pdp[200001],sdp[200001];
pair <int,int> hey[200001];
int go(int x) {
int i,pos=0;
for (i=17;i>=0;i--) {
if (pos+(1<<i)>n)
continue;
if (hey[pos+(1<<i)].first<x)
pos+=(1<<i);
}
return num[hey[pos].second];
}
int gogo(int x) {
int i,pos=0;
for (i=17;i>=0;i--) {
if (pos+(1<<i)>n)
continue;
if (hey[pos+(1<<i)].first<=x)
pos+=(1<<i);
}
if (pos==n)
return n+1;
return num[hey[pos+1].second];
}
void update(int pos,int val) {
pos+=N-1;
while (pos) {
tree[pos]=max(tree[pos],val);
pos/=2;
}
}
int solve(int p,int l,int r,int x,int y) {
if (x>r || l>y)
return 0;
if (l>=x && r<=y)
return tree[p];
return max(solve(2*p,l,(l+r)/2,x,y),solve(2*p+1,(l+r)/2+1,r,x,y));
}
int main() {
cin>>n>>d;
while (N<n)
N*=2;
for (i=1;i<=n;i++) {
cin>>mas[i];
hey[i]={mas[i],i};
}
sort(hey+1,hey+n+1);
for (i=1;i<=n;i++) {
if (hey[i].first>hey[i-1].first)
k++;
num[hey[i].second]=k;
}
for (i=1;i<=n;i++) {
pdp[i]=solve(1,N,2*N-1,N,N-1+num[i]-1)+1;
update(num[i],pdp[i]);
}
for (i=1;i<530000;i++)
tree[i]=0;
for (i=n;i>0;i--) {
sdp[i]=solve(1,N,2*N-1,N+num[i],2*N-1)+1;
update(num[i],sdp[i]);
}
for (i=1;i<530000;i++)
tree[i]=0;
for (i=1;i<=n;i++) {
ans=max(ans,solve(1,N,2*N-1,N,N-1+go(mas[i]+d))+sdp[i]);
update(num[i],pdp[i]);
}
for (i=1;i<530000;i++)
tree[i]=0;
for (i=n;i>0;i--) {
ans=max(ans,solve(1,N,2*N-1,N-1+gogo(mas[i]-d),2*N-1)+pdp[i]);
update(num[i],sdp[i]);
}
cout<<ans<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2424 KB |
Output is correct |
2 |
Correct |
4 ms |
2424 KB |
Output is correct |
3 |
Correct |
4 ms |
2424 KB |
Output is correct |
4 |
Correct |
4 ms |
2424 KB |
Output is correct |
5 |
Correct |
5 ms |
2424 KB |
Output is correct |
6 |
Correct |
4 ms |
2424 KB |
Output is correct |
7 |
Correct |
4 ms |
2552 KB |
Output is correct |
8 |
Correct |
4 ms |
2424 KB |
Output is correct |
9 |
Correct |
4 ms |
2424 KB |
Output is correct |
10 |
Correct |
4 ms |
2424 KB |
Output is correct |
11 |
Correct |
4 ms |
2424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2424 KB |
Output is correct |
2 |
Correct |
4 ms |
2424 KB |
Output is correct |
3 |
Correct |
4 ms |
2424 KB |
Output is correct |
4 |
Correct |
4 ms |
2424 KB |
Output is correct |
5 |
Correct |
5 ms |
2424 KB |
Output is correct |
6 |
Correct |
4 ms |
2424 KB |
Output is correct |
7 |
Correct |
4 ms |
2552 KB |
Output is correct |
8 |
Correct |
4 ms |
2424 KB |
Output is correct |
9 |
Correct |
4 ms |
2424 KB |
Output is correct |
10 |
Correct |
4 ms |
2424 KB |
Output is correct |
11 |
Correct |
4 ms |
2424 KB |
Output is correct |
12 |
Correct |
4 ms |
2424 KB |
Output is correct |
13 |
Correct |
4 ms |
2424 KB |
Output is correct |
14 |
Correct |
4 ms |
2424 KB |
Output is correct |
15 |
Correct |
4 ms |
2424 KB |
Output is correct |
16 |
Correct |
4 ms |
2424 KB |
Output is correct |
17 |
Correct |
4 ms |
2424 KB |
Output is correct |
18 |
Correct |
4 ms |
2424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2424 KB |
Output is correct |
2 |
Correct |
4 ms |
2424 KB |
Output is correct |
3 |
Correct |
4 ms |
2424 KB |
Output is correct |
4 |
Correct |
4 ms |
2424 KB |
Output is correct |
5 |
Correct |
5 ms |
2424 KB |
Output is correct |
6 |
Correct |
4 ms |
2424 KB |
Output is correct |
7 |
Correct |
4 ms |
2552 KB |
Output is correct |
8 |
Correct |
4 ms |
2424 KB |
Output is correct |
9 |
Correct |
4 ms |
2424 KB |
Output is correct |
10 |
Correct |
4 ms |
2424 KB |
Output is correct |
11 |
Correct |
4 ms |
2424 KB |
Output is correct |
12 |
Correct |
4 ms |
2424 KB |
Output is correct |
13 |
Correct |
4 ms |
2424 KB |
Output is correct |
14 |
Correct |
4 ms |
2424 KB |
Output is correct |
15 |
Correct |
4 ms |
2424 KB |
Output is correct |
16 |
Correct |
4 ms |
2424 KB |
Output is correct |
17 |
Correct |
4 ms |
2424 KB |
Output is correct |
18 |
Correct |
4 ms |
2424 KB |
Output is correct |
19 |
Correct |
5 ms |
2424 KB |
Output is correct |
20 |
Correct |
6 ms |
2424 KB |
Output is correct |
21 |
Correct |
5 ms |
2424 KB |
Output is correct |
22 |
Correct |
6 ms |
2460 KB |
Output is correct |
23 |
Correct |
5 ms |
2424 KB |
Output is correct |
24 |
Correct |
5 ms |
2456 KB |
Output is correct |
25 |
Correct |
6 ms |
2428 KB |
Output is correct |
26 |
Correct |
5 ms |
2424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
539 ms |
9100 KB |
Output is correct |
2 |
Correct |
635 ms |
9052 KB |
Output is correct |
3 |
Correct |
548 ms |
9156 KB |
Output is correct |
4 |
Correct |
557 ms |
9068 KB |
Output is correct |
5 |
Correct |
303 ms |
8312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
4064 KB |
Output is correct |
2 |
Correct |
115 ms |
4124 KB |
Output is correct |
3 |
Correct |
113 ms |
4088 KB |
Output is correct |
4 |
Correct |
67 ms |
3960 KB |
Output is correct |
5 |
Correct |
4 ms |
2424 KB |
Output is correct |
6 |
Correct |
66 ms |
3832 KB |
Output is correct |
7 |
Correct |
91 ms |
4216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
164 ms |
5692 KB |
Output is correct |
2 |
Correct |
164 ms |
5752 KB |
Output is correct |
3 |
Correct |
352 ms |
9104 KB |
Output is correct |
4 |
Correct |
262 ms |
8184 KB |
Output is correct |
5 |
Correct |
118 ms |
5368 KB |
Output is correct |
6 |
Correct |
235 ms |
8056 KB |
Output is correct |
7 |
Correct |
280 ms |
8696 KB |
Output is correct |
8 |
Correct |
155 ms |
5880 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
2424 KB |
Output is correct |
2 |
Correct |
4 ms |
2424 KB |
Output is correct |
3 |
Correct |
4 ms |
2424 KB |
Output is correct |
4 |
Correct |
4 ms |
2424 KB |
Output is correct |
5 |
Correct |
5 ms |
2424 KB |
Output is correct |
6 |
Correct |
4 ms |
2424 KB |
Output is correct |
7 |
Correct |
4 ms |
2552 KB |
Output is correct |
8 |
Correct |
4 ms |
2424 KB |
Output is correct |
9 |
Correct |
4 ms |
2424 KB |
Output is correct |
10 |
Correct |
4 ms |
2424 KB |
Output is correct |
11 |
Correct |
4 ms |
2424 KB |
Output is correct |
12 |
Correct |
4 ms |
2424 KB |
Output is correct |
13 |
Correct |
4 ms |
2424 KB |
Output is correct |
14 |
Correct |
4 ms |
2424 KB |
Output is correct |
15 |
Correct |
4 ms |
2424 KB |
Output is correct |
16 |
Correct |
4 ms |
2424 KB |
Output is correct |
17 |
Correct |
4 ms |
2424 KB |
Output is correct |
18 |
Correct |
4 ms |
2424 KB |
Output is correct |
19 |
Correct |
5 ms |
2424 KB |
Output is correct |
20 |
Correct |
6 ms |
2424 KB |
Output is correct |
21 |
Correct |
5 ms |
2424 KB |
Output is correct |
22 |
Correct |
6 ms |
2460 KB |
Output is correct |
23 |
Correct |
5 ms |
2424 KB |
Output is correct |
24 |
Correct |
5 ms |
2456 KB |
Output is correct |
25 |
Correct |
6 ms |
2428 KB |
Output is correct |
26 |
Correct |
5 ms |
2424 KB |
Output is correct |
27 |
Correct |
539 ms |
9100 KB |
Output is correct |
28 |
Correct |
635 ms |
9052 KB |
Output is correct |
29 |
Correct |
548 ms |
9156 KB |
Output is correct |
30 |
Correct |
557 ms |
9068 KB |
Output is correct |
31 |
Correct |
303 ms |
8312 KB |
Output is correct |
32 |
Correct |
117 ms |
4064 KB |
Output is correct |
33 |
Correct |
115 ms |
4124 KB |
Output is correct |
34 |
Correct |
113 ms |
4088 KB |
Output is correct |
35 |
Correct |
67 ms |
3960 KB |
Output is correct |
36 |
Correct |
4 ms |
2424 KB |
Output is correct |
37 |
Correct |
66 ms |
3832 KB |
Output is correct |
38 |
Correct |
91 ms |
4216 KB |
Output is correct |
39 |
Correct |
164 ms |
5692 KB |
Output is correct |
40 |
Correct |
164 ms |
5752 KB |
Output is correct |
41 |
Correct |
352 ms |
9104 KB |
Output is correct |
42 |
Correct |
262 ms |
8184 KB |
Output is correct |
43 |
Correct |
118 ms |
5368 KB |
Output is correct |
44 |
Correct |
235 ms |
8056 KB |
Output is correct |
45 |
Correct |
280 ms |
8696 KB |
Output is correct |
46 |
Correct |
155 ms |
5880 KB |
Output is correct |
47 |
Correct |
236 ms |
5672 KB |
Output is correct |
48 |
Correct |
251 ms |
5752 KB |
Output is correct |
49 |
Correct |
656 ms |
9004 KB |
Output is correct |
50 |
Correct |
303 ms |
8404 KB |
Output is correct |
51 |
Correct |
208 ms |
6904 KB |
Output is correct |
52 |
Correct |
318 ms |
8440 KB |
Output is correct |
53 |
Correct |
241 ms |
8312 KB |
Output is correct |
54 |
Correct |
282 ms |
9068 KB |
Output is correct |
55 |
Correct |
431 ms |
9028 KB |
Output is correct |