#include <bits/stdc++.h>
#define N 200005
using namespace std;
int t[N];
struct SegTree{
vector<int> t;
int n;
SegTree(int sz){
n = sz + 5;
t.assign(4*n,0);
}
void clear(){
t.assign(4*n,0);
}
void upd(int v,int tl,int tr,int pos,int val){
if(tl == tr){
t[v] = max(t[v],val);
return;
}
int tm = (tl + tr)/2;
if(pos <= tm)
upd(v*2,tl,tm,pos,val);
else upd(v*2+1,tm+1,tr,pos,val);
t[v] = max(t[v*2],t[v*2+1]);
}
int get(int v,int tl,int tr,int l,int r){
if(tr < l || r < tl)
return 0;
if(l <= tl && tr <= r){
return t[v];
}
int tm = (tl + tr)/2;
return max(get(v*2,tl,tm,l,r),get(v*2+1,tm+1,tr,l,r));
}
void upd(int pos,int val){
upd(1,1,n,pos,val);
}
int get(int l,int r){
return get(1,1,n,l,r);
}
};
pair<int,int> sufquery[N];
pair<int,int> prequery[N];
void solve(){
int n,x;
cin >> n >> x;
int best = 0;
vector<int> compress;
for(int i = 1;i<=n;i++){
cin >> t[i];
compress.push_back(t[i]);
compress.push_back(t[i]+x);
compress.push_back(t[i]-x);
}
sort(compress.begin(),compress.end());
compress.resize(unique(compress.begin(),compress.end())-compress.begin());
int sz = compress.size();
SegTree tpre(sz);
SegTree tsuf(sz);
for(int i = 1;i<=n;i++){
int num = upper_bound(compress.begin(),compress.end(),t[i] -x) - compress.begin();
tpre.upd(num,tpre.get(1,num-1)+1);
best = max(best,tpre.get(1,sz));
sufquery[i+1] = {tpre.get(num,num),num};
}
//cout << best << endl;
for(int i = n;i>=1;i--){
int num = upper_bound(compress.begin(),compress.end(),t[i]+x) - compress.begin();
tsuf.upd(num,tsuf.get(num+1,sz)+1);
best = max(best,tsuf.get(1,sz));
prequery[i-1] = {tsuf.get(num,num),num};
}
//cout << best << endl;
tpre.clear();
tsuf.clear();
for(int i = 1;i<=n;i++){
int num = upper_bound(compress.begin(),compress.end(),t[i]) - compress.begin();
tpre.upd(num,tpre.get(1,num-1)+1);
best = max(best,prequery[i].first + tpre.get(1,prequery[i].second-1));
}
for(int i = n;i>=1;i--){
int num = upper_bound(compress.begin(),compress.end(),t[i]) - compress.begin();
tsuf.upd(num,tsuf.get(num+1,sz)+1);
best = max(best,sufquery[i].first + tsuf.get(sufquery[i].second+1,sz));
}
cout << best;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int t = 1;
//cin >> t;
while(t--){
solve();
}
#ifdef Local
cout << endl << fixed << setprecision(2) << 1000.0*clock()/CLOCKS_PER_SEC << " milliseconds";
#endif
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 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 |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 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 |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 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 |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
3 ms |
468 KB |
Output is correct |
20 |
Correct |
3 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
666 ms |
14908 KB |
Output is correct |
2 |
Correct |
633 ms |
14968 KB |
Output is correct |
3 |
Correct |
638 ms |
15004 KB |
Output is correct |
4 |
Correct |
733 ms |
14908 KB |
Output is correct |
5 |
Correct |
364 ms |
10784 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
178 ms |
7224 KB |
Output is correct |
2 |
Correct |
167 ms |
7340 KB |
Output is correct |
3 |
Correct |
160 ms |
7200 KB |
Output is correct |
4 |
Correct |
118 ms |
4676 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
97 ms |
3884 KB |
Output is correct |
7 |
Correct |
126 ms |
6072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
332 ms |
13952 KB |
Output is correct |
2 |
Correct |
342 ms |
13964 KB |
Output is correct |
3 |
Correct |
725 ms |
27440 KB |
Output is correct |
4 |
Correct |
453 ms |
17256 KB |
Output is correct |
5 |
Correct |
275 ms |
13500 KB |
Output is correct |
6 |
Correct |
418 ms |
25360 KB |
Output is correct |
7 |
Correct |
421 ms |
26140 KB |
Output is correct |
8 |
Correct |
269 ms |
13976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 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 |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
3 ms |
468 KB |
Output is correct |
20 |
Correct |
3 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
2 ms |
340 KB |
Output is correct |
24 |
Correct |
2 ms |
340 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
666 ms |
14908 KB |
Output is correct |
28 |
Correct |
633 ms |
14968 KB |
Output is correct |
29 |
Correct |
638 ms |
15004 KB |
Output is correct |
30 |
Correct |
733 ms |
14908 KB |
Output is correct |
31 |
Correct |
364 ms |
10784 KB |
Output is correct |
32 |
Correct |
178 ms |
7224 KB |
Output is correct |
33 |
Correct |
167 ms |
7340 KB |
Output is correct |
34 |
Correct |
160 ms |
7200 KB |
Output is correct |
35 |
Correct |
118 ms |
4676 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
97 ms |
3884 KB |
Output is correct |
38 |
Correct |
126 ms |
6072 KB |
Output is correct |
39 |
Correct |
332 ms |
13952 KB |
Output is correct |
40 |
Correct |
342 ms |
13964 KB |
Output is correct |
41 |
Correct |
725 ms |
27440 KB |
Output is correct |
42 |
Correct |
453 ms |
17256 KB |
Output is correct |
43 |
Correct |
275 ms |
13500 KB |
Output is correct |
44 |
Correct |
418 ms |
25360 KB |
Output is correct |
45 |
Correct |
421 ms |
26140 KB |
Output is correct |
46 |
Correct |
269 ms |
13976 KB |
Output is correct |
47 |
Correct |
345 ms |
13956 KB |
Output is correct |
48 |
Correct |
343 ms |
14024 KB |
Output is correct |
49 |
Correct |
880 ms |
27424 KB |
Output is correct |
50 |
Correct |
429 ms |
17164 KB |
Output is correct |
51 |
Correct |
337 ms |
12368 KB |
Output is correct |
52 |
Correct |
479 ms |
14416 KB |
Output is correct |
53 |
Correct |
437 ms |
26980 KB |
Output is correct |
54 |
Correct |
451 ms |
27548 KB |
Output is correct |
55 |
Correct |
636 ms |
23336 KB |
Output is correct |