#include <bits/stdc++.h>
#include <unordered_set>
#define CHECK cout << "ok" << endl
#define finish(x) return cout << x << endl, 0
typedef long long ll;
typedef long double ldb;
const int md = 1e9 + 7, inf = 1e9 + 7;
using namespace std;
void debug(string names) {
cout << '\n';
}
template<typename A1, typename... A2>
void debug(string names, A1 par, A2... left) {
int pos = 0;
for (; pos < names.size() && names[pos] != ' ' && names[pos] != ','; pos++)
cout << names[pos];
cout << ": " << par << " ";
while (pos < names.size() && (names[pos] == ' ' || names[pos] == ',')) {
pos++;
}
names.erase(names.begin(), names.begin() + pos);
debug(names, left...);
}
int n, x;
vector<int> a;
vector<int> dp[2];
int L[2] = {};
vector<pair<int, int>> rev;
int upd(int ind, int val) {
int lo, hi, mid;
if (ind == 0) {
lo = 0, hi = L[0] + 1;
while (lo < hi) {
mid = (lo + hi) / 2;
if (val <= dp[0][mid]) hi = mid;
else lo = mid + 1;
}
rev.push_back({ lo, dp[0][lo] });
dp[0][lo] = val;
L[0] = max(L[0], lo);
}
else {
lo = 0, hi = L[1] + 1;
while (lo < hi) {
mid = (lo + hi) / 2;
if (val >= dp[1][mid]) hi = mid;
else lo = mid + 1;
}
dp[1][lo] = val;
L[1] = max(L[1], lo);
}
return lo;
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> x;
a.resize(n);
dp[0].resize(n + 1, 2 * inf), dp[1].resize(n + 1, -2 * inf);
dp[0][0] = -2 * inf;
dp[1][0] = 2 * inf;
for (auto &i : a) cin >> i, upd(0, i);
int ans = L[0];
for (int i = n - 1, pos1, pos0; i >= 0; i--) {
pos1 = upd(1, a[i]);
pos0 = rev.back().first;
dp[0][rev.back().first] = rev.back().second;
if (rev.back().second == 2 * inf) L[0]--;
rev.pop_back();
// cout << "i " << i << '\n';
// for (int j = 0; j <= L[0] + 1; j++) cout << dp[0][j] << " "; cout << '\n';
// for (int j = 0; j <= L[1] + 1; j++) cout << dp[1][j] << " "; cout << '\n';
int lo, hi, mid;
lo = 0, hi = L[1];
while (lo < hi) {
mid = (lo + hi + 1) / 2;
if (dp[0][pos0] - x < dp[1][mid]) lo = mid;
else hi = mid - 1;
}
// cout << "try 1: " << pos0 << " + " << lo << '\n';
ans = max(ans, pos0 + lo);
lo = 0, hi = L[0];
while (lo < hi) {
mid = (lo + hi + 1) / 2;
if (dp[1][pos1] + x > dp[0][mid]) lo = mid;
else hi = mid - 1;
}
// cout << "try 2: " << pos1 << " + " << lo << '\n';
ans = max(ans, pos1 + lo);
}
finish(ans);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
252 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
252 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
380 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
252 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
380 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
380 KB |
Output is correct |
21 |
Correct |
2 ms |
380 KB |
Output is correct |
22 |
Correct |
2 ms |
380 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
4976 KB |
Output is correct |
2 |
Correct |
75 ms |
4848 KB |
Output is correct |
3 |
Correct |
78 ms |
4848 KB |
Output is correct |
4 |
Correct |
76 ms |
4980 KB |
Output is correct |
5 |
Correct |
50 ms |
4976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
1528 KB |
Output is correct |
2 |
Correct |
19 ms |
1656 KB |
Output is correct |
3 |
Correct |
19 ms |
1540 KB |
Output is correct |
4 |
Correct |
12 ms |
1528 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
13 ms |
1784 KB |
Output is correct |
7 |
Correct |
16 ms |
2040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
2932 KB |
Output is correct |
2 |
Correct |
29 ms |
2804 KB |
Output is correct |
3 |
Correct |
57 ms |
4976 KB |
Output is correct |
4 |
Correct |
50 ms |
5012 KB |
Output is correct |
5 |
Correct |
24 ms |
3312 KB |
Output is correct |
6 |
Correct |
42 ms |
5744 KB |
Output is correct |
7 |
Correct |
49 ms |
6256 KB |
Output is correct |
8 |
Correct |
27 ms |
3572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
2 ms |
376 KB |
Output is correct |
6 |
Correct |
2 ms |
376 KB |
Output is correct |
7 |
Correct |
2 ms |
376 KB |
Output is correct |
8 |
Correct |
2 ms |
376 KB |
Output is correct |
9 |
Correct |
2 ms |
252 KB |
Output is correct |
10 |
Correct |
2 ms |
376 KB |
Output is correct |
11 |
Correct |
2 ms |
376 KB |
Output is correct |
12 |
Correct |
2 ms |
380 KB |
Output is correct |
13 |
Correct |
2 ms |
376 KB |
Output is correct |
14 |
Correct |
2 ms |
376 KB |
Output is correct |
15 |
Correct |
2 ms |
376 KB |
Output is correct |
16 |
Correct |
2 ms |
376 KB |
Output is correct |
17 |
Correct |
2 ms |
376 KB |
Output is correct |
18 |
Correct |
2 ms |
376 KB |
Output is correct |
19 |
Correct |
2 ms |
376 KB |
Output is correct |
20 |
Correct |
2 ms |
380 KB |
Output is correct |
21 |
Correct |
2 ms |
380 KB |
Output is correct |
22 |
Correct |
2 ms |
380 KB |
Output is correct |
23 |
Correct |
2 ms |
376 KB |
Output is correct |
24 |
Correct |
2 ms |
376 KB |
Output is correct |
25 |
Correct |
2 ms |
376 KB |
Output is correct |
26 |
Correct |
2 ms |
376 KB |
Output is correct |
27 |
Correct |
75 ms |
4976 KB |
Output is correct |
28 |
Correct |
75 ms |
4848 KB |
Output is correct |
29 |
Correct |
78 ms |
4848 KB |
Output is correct |
30 |
Correct |
76 ms |
4980 KB |
Output is correct |
31 |
Correct |
50 ms |
4976 KB |
Output is correct |
32 |
Correct |
19 ms |
1528 KB |
Output is correct |
33 |
Correct |
19 ms |
1656 KB |
Output is correct |
34 |
Correct |
19 ms |
1540 KB |
Output is correct |
35 |
Correct |
12 ms |
1528 KB |
Output is correct |
36 |
Correct |
2 ms |
376 KB |
Output is correct |
37 |
Correct |
13 ms |
1784 KB |
Output is correct |
38 |
Correct |
16 ms |
2040 KB |
Output is correct |
39 |
Correct |
30 ms |
2932 KB |
Output is correct |
40 |
Correct |
29 ms |
2804 KB |
Output is correct |
41 |
Correct |
57 ms |
4976 KB |
Output is correct |
42 |
Correct |
50 ms |
5012 KB |
Output is correct |
43 |
Correct |
24 ms |
3312 KB |
Output is correct |
44 |
Correct |
42 ms |
5744 KB |
Output is correct |
45 |
Correct |
49 ms |
6256 KB |
Output is correct |
46 |
Correct |
27 ms |
3572 KB |
Output is correct |
47 |
Correct |
38 ms |
3700 KB |
Output is correct |
48 |
Correct |
39 ms |
3572 KB |
Output is correct |
49 |
Correct |
81 ms |
6384 KB |
Output is correct |
50 |
Correct |
49 ms |
5872 KB |
Output is correct |
51 |
Correct |
42 ms |
5232 KB |
Output is correct |
52 |
Correct |
49 ms |
5920 KB |
Output is correct |
53 |
Correct |
44 ms |
5872 KB |
Output is correct |
54 |
Correct |
50 ms |
6384 KB |
Output is correct |
55 |
Correct |
65 ms |
6524 KB |
Output is correct |