#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
const int MAX = 4e5 + 10;
int n, k;
int x, y;
int ucit[MAX];
int arr[MAX];
int treeFront[4 * MAX];
int treeBack[4 * MAX];
int dpFront[MAX];
int dpBack[MAX];
vector <int> v;
map <int, int> mp;
int id;
void updateFront(int left, int right, int a, int idx, int val) {
if (left > a || a > right) return;
if (left == right) {
treeFront[idx] = max(treeFront[idx], val);
return;
}
int mid = (left + right) / 2;
updateFront(left, mid, a, idx * 2, val);
updateFront(mid + 1, right, a, idx * 2 + 1, val);
treeFront[idx] = max(treeFront[idx * 2], treeFront[idx * 2 + 1]);
}
void updateBack(int left, int right, int a, int idx, int val) {
if (left > a || a > right) return;
if (left == right) {
treeBack[idx] = max(treeBack[idx], val);
return;
}
int mid = (left + right) / 2;
updateBack(left, mid, a, idx * 2, val);
updateBack(mid + 1, right, a, idx * 2 + 1, val);
treeBack[idx] = max(treeBack[idx * 2], treeBack[idx * 2 + 1]);
}
int queryFront(int left, int right, int a, int idx) {
if (left > a) return 0;
if (a >= right) {
return treeFront[idx];
}
int mid = (left + right) / 2;
int ret1 = queryFront(left, mid, a, idx * 2);
int ret2 = queryFront(mid + 1, right, a, idx * 2 + 1);
return max(ret1, ret2);
}
int queryBack(int left, int right, int a, int idx) {
if (right < a) return 0;
if (a <= left) {
return treeBack[idx];
}
int mid = (left + right) / 2;
int ret1 = queryBack(left, mid, a, idx * 2);
int ret2 = queryBack(mid + 1, right, a, idx * 2 + 1);
return max(ret1, ret2);
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> ucit[i];
v.push_back(ucit[i]);
v.push_back(ucit[i] + k);
}
sort(v.begin(), v.end());
mp[v[0]] = 0;
id = 1;
for (int i = 1; i < v.size(); i++) {
if (v[i] == v[i - 1]) continue;
mp[v[i]] = id;
id++;
}
for (int i = 1; i <= n; i++) {
arr[i] = mp[ucit[i]];
}
for (int i = n; i >= 1; i--) {
x = arr[i];
dpBack[i] = queryBack(0, 2 * n - 1, x + 1, 1) + 1;
updateBack(0, 2 * n - 1, x, 1, dpBack[i]);
}
int sol = queryBack(0, 2 * n - 1, -1, 1);
for (int i = 1; i < n; i++) {
x = arr[i];
dpFront[i] = queryFront(0, 2 * n - 1, x - 1, 1) + 1;
updateFront(0, 2 * n - 1, x, 1, dpFront[i]);
sol = max(sol, queryFront(0, 2 * n - 1, mp[ucit[i + 1] + k] - 1 , 1) + dpBack[i + 1]);
}
cout << sol << "\n";
return 0;
}
Compilation message
glo.cpp: In function 'int main()':
glo.cpp:92:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for (int i = 1; i < v.size(); i++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
2 ms |
460 KB |
Output is correct |
20 |
Correct |
2 ms |
460 KB |
Output is correct |
21 |
Correct |
2 ms |
460 KB |
Output is correct |
22 |
Correct |
2 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
460 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
660 ms |
18700 KB |
Output is correct |
2 |
Correct |
675 ms |
18536 KB |
Output is correct |
3 |
Correct |
636 ms |
18556 KB |
Output is correct |
4 |
Correct |
589 ms |
18624 KB |
Output is correct |
5 |
Correct |
256 ms |
11864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
128 ms |
8200 KB |
Output is correct |
2 |
Correct |
160 ms |
8244 KB |
Output is correct |
3 |
Correct |
134 ms |
8160 KB |
Output is correct |
4 |
Correct |
65 ms |
4840 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
85 ms |
5120 KB |
Output is correct |
7 |
Correct |
122 ms |
7600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
287 ms |
14168 KB |
Output is correct |
2 |
Correct |
312 ms |
14848 KB |
Output is correct |
3 |
Correct |
672 ms |
29580 KB |
Output is correct |
4 |
Correct |
310 ms |
16504 KB |
Output is correct |
5 |
Correct |
185 ms |
14192 KB |
Output is correct |
6 |
Correct |
360 ms |
26824 KB |
Output is correct |
7 |
Correct |
366 ms |
26716 KB |
Output is correct |
8 |
Correct |
218 ms |
14140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
0 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
320 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
316 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
2 ms |
460 KB |
Output is correct |
20 |
Correct |
2 ms |
460 KB |
Output is correct |
21 |
Correct |
2 ms |
460 KB |
Output is correct |
22 |
Correct |
2 ms |
460 KB |
Output is correct |
23 |
Correct |
1 ms |
460 KB |
Output is correct |
24 |
Correct |
1 ms |
460 KB |
Output is correct |
25 |
Correct |
1 ms |
332 KB |
Output is correct |
26 |
Correct |
2 ms |
460 KB |
Output is correct |
27 |
Correct |
660 ms |
18700 KB |
Output is correct |
28 |
Correct |
675 ms |
18536 KB |
Output is correct |
29 |
Correct |
636 ms |
18556 KB |
Output is correct |
30 |
Correct |
589 ms |
18624 KB |
Output is correct |
31 |
Correct |
256 ms |
11864 KB |
Output is correct |
32 |
Correct |
128 ms |
8200 KB |
Output is correct |
33 |
Correct |
160 ms |
8244 KB |
Output is correct |
34 |
Correct |
134 ms |
8160 KB |
Output is correct |
35 |
Correct |
65 ms |
4840 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
85 ms |
5120 KB |
Output is correct |
38 |
Correct |
122 ms |
7600 KB |
Output is correct |
39 |
Correct |
287 ms |
14168 KB |
Output is correct |
40 |
Correct |
312 ms |
14848 KB |
Output is correct |
41 |
Correct |
672 ms |
29580 KB |
Output is correct |
42 |
Correct |
310 ms |
16504 KB |
Output is correct |
43 |
Correct |
185 ms |
14192 KB |
Output is correct |
44 |
Correct |
360 ms |
26824 KB |
Output is correct |
45 |
Correct |
366 ms |
26716 KB |
Output is correct |
46 |
Correct |
218 ms |
14140 KB |
Output is correct |
47 |
Correct |
370 ms |
17188 KB |
Output is correct |
48 |
Correct |
403 ms |
17112 KB |
Output is correct |
49 |
Correct |
893 ms |
33904 KB |
Output is correct |
50 |
Correct |
317 ms |
19804 KB |
Output is correct |
51 |
Correct |
270 ms |
14600 KB |
Output is correct |
52 |
Correct |
361 ms |
19908 KB |
Output is correct |
53 |
Correct |
422 ms |
29364 KB |
Output is correct |
54 |
Correct |
416 ms |
29944 KB |
Output is correct |
55 |
Correct |
561 ms |
29500 KB |
Output is correct |