#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
const int N = 2e5+10;
struct segtree {
vector<int> t;
int sz;
void init(int k) {
sz=1;
while (sz < 4*k) sz*=2;
t.resize(sz+1);
}
void upd(int v, int l, int r, int p, int val) {
if (l == r) {
t[v]=max(t[v], val);
return;
} int m=(l+r)/2;
if (p <= m) upd(2*v, l, m, p, val);
else upd(2*v+1, m+1, r, p, val);
t[v]=max(t[2*v], t[2*v+1]);
}
int que(int v, int l, int r, int ql, int qr) {
if (l > qr || r < ql) return 0;
if (l >= ql && r <= qr) return t[v];
int m=(l+r)/2;
return max(que(2*v, l, m, ql, qr), que(2*v+1, m+1, r, ql, qr));
}
};
int a[N], b[N], n, x, pre[N], suf[N];
unordered_map<int, int> sc;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(NULL);
cout.tie(NULL);
cin>>n>>x;
set<int> st;
for (int i=1; i<=n; ++i) {
cin>>a[i];
st.insert(a[i]), st.insert(a[i]+x);
}
int i=1;
for (auto u : st) sc[u]=i++;
for (int i=1; i<=n; ++i) b[i]=sc[a[i]];
segtree tr2; tr2.init(2*n+10);
for (int i=n; i>=1; --i) {
suf[i]=tr2.que(1, 1, 2*n, b[i]+1, 2*n)+1;
tr2.upd(1, 1, 2*n, b[i], suf[i]);
}
/*
cout<<"pre: ";
for (int i=1; i<=n; ++i) cout<<pre[i]<<" ";
cout<<"\n";
cout<<"suf: ";
for (int i=1; i<=n; ++i) cout<<suf[i]<<" ";
cout<<"\n";
*/
int ans=0;
segtree tr; tr.init(2*n+1);
for (int i=1; i+1<=n; ++i) {
pre[i]=tr.que(1, 1, 2*n, 1, b[i]-1)+1;
tr.upd(1, 1, 2*n, b[i], pre[i]);
ans=max(ans, tr.que(1, 1, 2*n, 1, sc[a[i+1]+x]-1)+suf[i+1]);
} cout<<ans<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
456 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
285 ms |
38268 KB |
Output is correct |
2 |
Correct |
342 ms |
40176 KB |
Output is correct |
3 |
Correct |
291 ms |
40080 KB |
Output is correct |
4 |
Correct |
288 ms |
40208 KB |
Output is correct |
5 |
Correct |
153 ms |
30200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
14380 KB |
Output is correct |
2 |
Correct |
77 ms |
14844 KB |
Output is correct |
3 |
Correct |
70 ms |
14852 KB |
Output is correct |
4 |
Correct |
34 ms |
10060 KB |
Output is correct |
5 |
Correct |
1 ms |
328 KB |
Output is correct |
6 |
Correct |
34 ms |
10096 KB |
Output is correct |
7 |
Correct |
52 ms |
12860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
207 ms |
28500 KB |
Output is correct |
2 |
Correct |
192 ms |
29380 KB |
Output is correct |
3 |
Correct |
484 ms |
58648 KB |
Output is correct |
4 |
Correct |
195 ms |
39412 KB |
Output is correct |
5 |
Correct |
102 ms |
29028 KB |
Output is correct |
6 |
Correct |
196 ms |
56112 KB |
Output is correct |
7 |
Correct |
204 ms |
56788 KB |
Output is correct |
8 |
Correct |
147 ms |
29436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
596 KB |
Output is correct |
20 |
Correct |
1 ms |
596 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
1 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
1 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
456 KB |
Output is correct |
27 |
Correct |
285 ms |
38268 KB |
Output is correct |
28 |
Correct |
342 ms |
40176 KB |
Output is correct |
29 |
Correct |
291 ms |
40080 KB |
Output is correct |
30 |
Correct |
288 ms |
40208 KB |
Output is correct |
31 |
Correct |
153 ms |
30200 KB |
Output is correct |
32 |
Correct |
72 ms |
14380 KB |
Output is correct |
33 |
Correct |
77 ms |
14844 KB |
Output is correct |
34 |
Correct |
70 ms |
14852 KB |
Output is correct |
35 |
Correct |
34 ms |
10060 KB |
Output is correct |
36 |
Correct |
1 ms |
328 KB |
Output is correct |
37 |
Correct |
34 ms |
10096 KB |
Output is correct |
38 |
Correct |
52 ms |
12860 KB |
Output is correct |
39 |
Correct |
207 ms |
28500 KB |
Output is correct |
40 |
Correct |
192 ms |
29380 KB |
Output is correct |
41 |
Correct |
484 ms |
58648 KB |
Output is correct |
42 |
Correct |
195 ms |
39412 KB |
Output is correct |
43 |
Correct |
102 ms |
29028 KB |
Output is correct |
44 |
Correct |
196 ms |
56112 KB |
Output is correct |
45 |
Correct |
204 ms |
56788 KB |
Output is correct |
46 |
Correct |
147 ms |
29436 KB |
Output is correct |
47 |
Correct |
205 ms |
29452 KB |
Output is correct |
48 |
Correct |
164 ms |
29428 KB |
Output is correct |
49 |
Correct |
518 ms |
58716 KB |
Output is correct |
50 |
Correct |
172 ms |
39428 KB |
Output is correct |
51 |
Correct |
121 ms |
33000 KB |
Output is correct |
52 |
Correct |
168 ms |
39596 KB |
Output is correct |
53 |
Correct |
216 ms |
57996 KB |
Output is correct |
54 |
Correct |
211 ms |
58624 KB |
Output is correct |
55 |
Correct |
293 ms |
50668 KB |
Output is correct |