#include <bits/stdc++.h>
using namespace std;
const int N = (int) 2e5 + 10;
int n, x;
int a[N];
int b[4][N];
int n_node;
int tree[2][N * 4];
int prf[N][2];
int res;
void Update(int id, int idx, int val) {
for( ; idx <= n_node; idx += (idx & - idx)) tree[id][idx] = max(tree[id][idx], val);
}
int Query(int id, int idx) {
int res = 0;
for( ; idx > 0; idx -= (idx & - idx)) res = max(res, tree[id][idx]);
return res;
}
void reset(int id, int idx) {
for ( ; idx <= n_node; idx += (idx & - idx)) tree[id][idx] = 0;
}
void compress() {
vector<int> Pos = {};
for (int i = 1; i <= n; ++ i) {
Pos.push_back(b[0][i]);
Pos.push_back(b[1][i]);
Pos.push_back(b[2][i]);
Pos.push_back(b[3][i]);
}
sort(Pos.begin(), Pos.end());
Pos.resize(unique(Pos.begin(), Pos.end()) - Pos.begin());
for (int i = 1; i <= n; ++ i) {
b[0][i] = lower_bound(Pos.begin(), Pos.end(), b[0][i]) - Pos.begin() + 1;
b[1][i] = lower_bound(Pos.begin(), Pos.end(), b[1][i]) - Pos.begin() + 1;
b[2][i] = lower_bound(Pos.begin(), Pos.end(), b[2][i]) - Pos.begin() + 1;
b[3][i] = lower_bound(Pos.begin(), Pos.end(), b[3][i]) - Pos.begin() + 1;
}
}
void solve() {
for (int i = 1; i <= n; ++ i) {
b[0][i] = a[i];
b[1][i] = a[i] - x;
b[2][i] = -b[0][i];
b[3][i] = -b[1][i];
}
compress();
n_node = 4 * n;
for (int i = 1; i <= n; ++ i) {
int val[2] = {b[0][i], b[1][i]};
prf[i][0] = Query(0, val[0]);
Update(0, val[0] + 1, prf[i][0] + 1);
prf[i][0] = max(prf[i][0], Query(1, val[0]));
prf[i][1] = Query(1, val[1]);
Update(1, val[1] + 1, prf[i][1] + 1);
}
for (int i = 1; i <= n; ++ i) {
reset(0, b[0][i] + 1);
reset(1, b[1][i] + 1);
}
for (int i = n; i >= 1; -- i) {
int best[2] = {Query(0, b[2][i]), Query(0, b[3][i])};
res = max(res, best[0] + prf[i][0] + 1);
res = max(res, best[1] + prf[i][1] + 1);
Update(0, b[2][i] + 1, best[0] + 1);
}
for (int i = 1; i <= n; ++ i) {
reset(0, b[2][i] + 1);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cin >> n >> x;
for (int i = 1; i <= n; ++ i) cin >> a[i];
solve();
reverse(a + 1, a + 1 + n);
for (int i = 1; i <= n; ++ i) a[i] = -a[i];
solve();
cout << res;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
440 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
440 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
2 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
2 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
370 ms |
17544 KB |
Output is correct |
2 |
Correct |
365 ms |
17436 KB |
Output is correct |
3 |
Correct |
378 ms |
17264 KB |
Output is correct |
4 |
Correct |
388 ms |
17488 KB |
Output is correct |
5 |
Correct |
173 ms |
15396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
5324 KB |
Output is correct |
2 |
Correct |
82 ms |
5308 KB |
Output is correct |
3 |
Correct |
97 ms |
5304 KB |
Output is correct |
4 |
Correct |
41 ms |
4548 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
47 ms |
4552 KB |
Output is correct |
7 |
Correct |
64 ms |
5176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
199 ms |
10140 KB |
Output is correct |
2 |
Correct |
203 ms |
10156 KB |
Output is correct |
3 |
Correct |
443 ms |
19568 KB |
Output is correct |
4 |
Correct |
195 ms |
16672 KB |
Output is correct |
5 |
Correct |
112 ms |
8620 KB |
Output is correct |
6 |
Correct |
195 ms |
16036 KB |
Output is correct |
7 |
Correct |
204 ms |
16652 KB |
Output is correct |
8 |
Correct |
151 ms |
9624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
600 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
2 ms |
440 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
2 ms |
600 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
2 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
370 ms |
17544 KB |
Output is correct |
28 |
Correct |
365 ms |
17436 KB |
Output is correct |
29 |
Correct |
378 ms |
17264 KB |
Output is correct |
30 |
Correct |
388 ms |
17488 KB |
Output is correct |
31 |
Correct |
173 ms |
15396 KB |
Output is correct |
32 |
Correct |
87 ms |
5324 KB |
Output is correct |
33 |
Correct |
82 ms |
5308 KB |
Output is correct |
34 |
Correct |
97 ms |
5304 KB |
Output is correct |
35 |
Correct |
41 ms |
4548 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
47 ms |
4552 KB |
Output is correct |
38 |
Correct |
64 ms |
5176 KB |
Output is correct |
39 |
Correct |
199 ms |
10140 KB |
Output is correct |
40 |
Correct |
203 ms |
10156 KB |
Output is correct |
41 |
Correct |
443 ms |
19568 KB |
Output is correct |
42 |
Correct |
195 ms |
16672 KB |
Output is correct |
43 |
Correct |
112 ms |
8620 KB |
Output is correct |
44 |
Correct |
195 ms |
16036 KB |
Output is correct |
45 |
Correct |
204 ms |
16652 KB |
Output is correct |
46 |
Correct |
151 ms |
9624 KB |
Output is correct |
47 |
Correct |
177 ms |
10156 KB |
Output is correct |
48 |
Correct |
176 ms |
10152 KB |
Output is correct |
49 |
Correct |
388 ms |
19568 KB |
Output is correct |
50 |
Correct |
180 ms |
16652 KB |
Output is correct |
51 |
Correct |
155 ms |
12924 KB |
Output is correct |
52 |
Correct |
216 ms |
16776 KB |
Output is correct |
53 |
Correct |
219 ms |
16752 KB |
Output is correct |
54 |
Correct |
213 ms |
17468 KB |
Output is correct |
55 |
Correct |
298 ms |
19060 KB |
Output is correct |