#include <bits/stdc++.h>
#define ll long long
#define oo 1e9
#define pii pair<int, int>
using namespace std;
const int MAX = 2e5 + 5, treesize = MAX * 21, CAP = 1e9 + 9;
int n, x;
int arr[MAX];
int tree[treesize], L[treesize], R[treesize];
int LIS[MAX];
int nxt = 2;
void update(int node, int l, int r, int pos, int val){
if(l == r){
tree[node] = max(val, tree[node]);
return;
}
int mid = (l + r) / 2;
if(pos <= mid){
if(!L[node]) L[node] = nxt++;
update(L[node], l, mid, pos, val);
}
else{
if(!R[node]) R[node] = nxt++;
update(R[node], mid + 1, r, pos, val);
}
tree[node] = max(tree[L[node]], tree[R[node]]);
}
int ask(int node, int l, int r, int ql, int qr){
if(!node) return 0;
if(qr < l || r < ql) return 0;
if(ql <= l && r <= qr) return tree[node];
int mid = (l + r) / 2;
return max(ask(L[node], l, mid, ql, qr), ask(R[node], mid + 1, r, ql, qr));
}
void solve(){
cin >> n >> x;
for(int i = 1; i <= n; i++){
cin >> arr[i];
}
vector<int> v;
for(int i = 1; i <= n; i++){
int pos = lower_bound(v.begin(), v.end(), arr[i]) - v.begin();
LIS[i] = pos + 1;
if(pos == v.size()){
v.push_back(arr[i]);
}
else{
v[pos] = arr[i];
}
}
int ans = 0;
for(int i = n; i >= 1; i--){
ans = max(ans, LIS[i] + ask(1, 1, CAP, max(arr[i] - x + 1, 1), arr[i] + x - 1));
update(1, 1, CAP, arr[i], ask(1, 1, CAP, arr[i] + 1, CAP) + 1);
}
cout << ans << '\n';
}
int main()
{
int t = 1;
while(t){
t--;
solve();
}
}
Compilation message
glo.cpp: In function 'void solve()':
glo.cpp:52:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | if(pos == v.size()){
| ~~~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
448 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
448 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
444 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
448 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
444 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
620 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
2 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
267 ms |
35412 KB |
Output is correct |
2 |
Correct |
258 ms |
35396 KB |
Output is correct |
3 |
Correct |
292 ms |
35316 KB |
Output is correct |
4 |
Correct |
354 ms |
35408 KB |
Output is correct |
5 |
Correct |
148 ms |
20788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
10832 KB |
Output is correct |
2 |
Correct |
54 ms |
10788 KB |
Output is correct |
3 |
Correct |
56 ms |
10840 KB |
Output is correct |
4 |
Correct |
37 ms |
6536 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
38 ms |
3760 KB |
Output is correct |
7 |
Correct |
47 ms |
8276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
91 ms |
19284 KB |
Output is correct |
2 |
Correct |
91 ms |
19384 KB |
Output is correct |
3 |
Correct |
232 ms |
35284 KB |
Output is correct |
4 |
Correct |
139 ms |
20576 KB |
Output is correct |
5 |
Correct |
58 ms |
5292 KB |
Output is correct |
6 |
Correct |
106 ms |
8060 KB |
Output is correct |
7 |
Correct |
119 ms |
8652 KB |
Output is correct |
8 |
Correct |
86 ms |
14968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
448 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
492 KB |
Output is correct |
13 |
Correct |
1 ms |
600 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
444 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
620 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
464 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
2 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
267 ms |
35412 KB |
Output is correct |
28 |
Correct |
258 ms |
35396 KB |
Output is correct |
29 |
Correct |
292 ms |
35316 KB |
Output is correct |
30 |
Correct |
354 ms |
35408 KB |
Output is correct |
31 |
Correct |
148 ms |
20788 KB |
Output is correct |
32 |
Correct |
50 ms |
10832 KB |
Output is correct |
33 |
Correct |
54 ms |
10788 KB |
Output is correct |
34 |
Correct |
56 ms |
10840 KB |
Output is correct |
35 |
Correct |
37 ms |
6536 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
38 ms |
3760 KB |
Output is correct |
38 |
Correct |
47 ms |
8276 KB |
Output is correct |
39 |
Correct |
91 ms |
19284 KB |
Output is correct |
40 |
Correct |
91 ms |
19384 KB |
Output is correct |
41 |
Correct |
232 ms |
35284 KB |
Output is correct |
42 |
Correct |
139 ms |
20576 KB |
Output is correct |
43 |
Correct |
58 ms |
5292 KB |
Output is correct |
44 |
Correct |
106 ms |
8060 KB |
Output is correct |
45 |
Correct |
119 ms |
8652 KB |
Output is correct |
46 |
Correct |
86 ms |
14968 KB |
Output is correct |
47 |
Correct |
114 ms |
19420 KB |
Output is correct |
48 |
Correct |
124 ms |
19472 KB |
Output is correct |
49 |
Correct |
309 ms |
35200 KB |
Output is correct |
50 |
Correct |
140 ms |
20428 KB |
Output is correct |
51 |
Correct |
113 ms |
6184 KB |
Output is correct |
52 |
Correct |
169 ms |
8256 KB |
Output is correct |
53 |
Correct |
115 ms |
8704 KB |
Output is correct |
54 |
Correct |
137 ms |
9160 KB |
Output is correct |
55 |
Correct |
196 ms |
26452 KB |
Output is correct |