#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ar array
template <class T>
bool minimize(T &a, const T &b) {
if(a > b) {a = b; return 1;}
return 0;
}
template <class T>
bool maximize(T &a, const T &b) {
if(a < b) {a = b; return 1;}
return 0;
}
const int N = 4e5 + 7, oo = 1e9 + 7;
int n, m, ans, a[N], T[4 * N], lis[N], lds[N], val[N];
void upd(int id, int l, int r, int pos, int val) {
if (l > pos || r < pos) return;
if (l == r) {
maximize(T[id], val);
return;
}
int mid = l + r >> 1, k = id << 1;
upd(k, l, mid, pos, val); upd(k | 1, mid + 1, r, pos, val);
T[id] = max(T[k], T[k | 1]);
}
int get(int id, int l, int r, int u, int v) {
if (l > v || r < u) return -oo;
if (u <= l && r <= v) return T[id];
int mid = l + r >> 1, k = id << 1;
return max(get(k, l, mid, u, v), get(k | 1, mid + 1, r, u, v));
}
int get(int l, int r) {
return get(1, 1, N - 1, l, r);
}
void upd(int pos, int val) {
upd(1, 1, N - 1, pos, val);
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
if (fopen("test.inp", "r"))
freopen("test.inp", "r", stdin);
cin >> n >> m; vector<int> v, vec;
for (int i = 1; i <= n; i++) {
cin >> a[i];
v.push_back(a[i] - m + 1);
v.push_back(a[i] + m - 1);
v.push_back(a[i]);
}
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
for (int i = 1; i <= n; i++) {
val[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin() + 1;
int pos = lower_bound(vec.begin(), vec.end(), val[i]) - vec.begin();
if (pos == vec.size()) vec.push_back(val[i]); else vec[pos] = val[i];
lis[i] = pos + 1; maximize(ans, lis[i]);
}
vec.clear();
for (int i = n; i >= 1; i--) {
int pos = lower_bound(vec.begin(), vec.end(), -val[i]) - vec.begin();
if (pos == vec.size()) vec.push_back(-val[i]); else vec[pos] = -val[i];
lds[i] = pos + 1;
}
for (int i = n; i >= 1; i--) {
int num = lower_bound(v.begin(), v.end(), a[i] - m + 1) - v.begin() + 1;
maximize(ans, lis[i] + get(num, N - 1));
upd(val[i], lds[i]);
}
memset(T, 0, sizeof T);
for (int i = 1; i <= n; i++) {
int num = upper_bound(v.begin(), v.end(), a[i] + m - 1) - v.begin();
maximize(ans, lds[i] + get(1, num));
upd(val[i], lis[i]);
}
cout << ans;
}
Compilation message
glo.cpp: In function 'void upd(int, int, int, int, int)':
glo.cpp:29:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
29 | int mid = l + r >> 1, k = id << 1;
| ~~^~~
glo.cpp: In function 'int get(int, int, int, int, int)':
glo.cpp:38:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
38 | int mid = l + r >> 1, k = id << 1;
| ~~^~~
glo.cpp: In function 'int main()':
glo.cpp:68:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | if (pos == vec.size()) vec.push_back(val[i]); else vec[pos] = val[i];
| ~~~~^~~~~~~~~~~~~
glo.cpp:75:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | if (pos == vec.size()) vec.push_back(-val[i]); else vec[pos] = -val[i];
| ~~~~^~~~~~~~~~~~~
glo.cpp:53:9: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
53 | freopen("test.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
3 ms |
6484 KB |
Output is correct |
4 |
Correct |
3 ms |
6612 KB |
Output is correct |
5 |
Correct |
2 ms |
6612 KB |
Output is correct |
6 |
Correct |
3 ms |
6600 KB |
Output is correct |
7 |
Correct |
2 ms |
6612 KB |
Output is correct |
8 |
Correct |
3 ms |
6484 KB |
Output is correct |
9 |
Correct |
3 ms |
6612 KB |
Output is correct |
10 |
Correct |
2 ms |
6484 KB |
Output is correct |
11 |
Correct |
3 ms |
6612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
3 ms |
6484 KB |
Output is correct |
4 |
Correct |
3 ms |
6612 KB |
Output is correct |
5 |
Correct |
2 ms |
6612 KB |
Output is correct |
6 |
Correct |
3 ms |
6600 KB |
Output is correct |
7 |
Correct |
2 ms |
6612 KB |
Output is correct |
8 |
Correct |
3 ms |
6484 KB |
Output is correct |
9 |
Correct |
3 ms |
6612 KB |
Output is correct |
10 |
Correct |
2 ms |
6484 KB |
Output is correct |
11 |
Correct |
3 ms |
6612 KB |
Output is correct |
12 |
Correct |
3 ms |
6612 KB |
Output is correct |
13 |
Correct |
3 ms |
6612 KB |
Output is correct |
14 |
Correct |
4 ms |
6612 KB |
Output is correct |
15 |
Correct |
3 ms |
6592 KB |
Output is correct |
16 |
Correct |
4 ms |
6612 KB |
Output is correct |
17 |
Correct |
3 ms |
6596 KB |
Output is correct |
18 |
Correct |
3 ms |
6612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
3 ms |
6484 KB |
Output is correct |
4 |
Correct |
3 ms |
6612 KB |
Output is correct |
5 |
Correct |
2 ms |
6612 KB |
Output is correct |
6 |
Correct |
3 ms |
6600 KB |
Output is correct |
7 |
Correct |
2 ms |
6612 KB |
Output is correct |
8 |
Correct |
3 ms |
6484 KB |
Output is correct |
9 |
Correct |
3 ms |
6612 KB |
Output is correct |
10 |
Correct |
2 ms |
6484 KB |
Output is correct |
11 |
Correct |
3 ms |
6612 KB |
Output is correct |
12 |
Correct |
3 ms |
6612 KB |
Output is correct |
13 |
Correct |
3 ms |
6612 KB |
Output is correct |
14 |
Correct |
4 ms |
6612 KB |
Output is correct |
15 |
Correct |
3 ms |
6592 KB |
Output is correct |
16 |
Correct |
4 ms |
6612 KB |
Output is correct |
17 |
Correct |
3 ms |
6596 KB |
Output is correct |
18 |
Correct |
3 ms |
6612 KB |
Output is correct |
19 |
Correct |
4 ms |
6612 KB |
Output is correct |
20 |
Correct |
4 ms |
6608 KB |
Output is correct |
21 |
Correct |
4 ms |
6612 KB |
Output is correct |
22 |
Correct |
4 ms |
6612 KB |
Output is correct |
23 |
Correct |
4 ms |
6612 KB |
Output is correct |
24 |
Correct |
3 ms |
6604 KB |
Output is correct |
25 |
Correct |
3 ms |
6612 KB |
Output is correct |
26 |
Correct |
4 ms |
6612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
316 ms |
12160 KB |
Output is correct |
2 |
Correct |
321 ms |
14072 KB |
Output is correct |
3 |
Correct |
310 ms |
14112 KB |
Output is correct |
4 |
Correct |
309 ms |
14136 KB |
Output is correct |
5 |
Correct |
170 ms |
14120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
76 ms |
8004 KB |
Output is correct |
2 |
Correct |
81 ms |
8528 KB |
Output is correct |
3 |
Correct |
100 ms |
8484 KB |
Output is correct |
4 |
Correct |
43 ms |
8472 KB |
Output is correct |
5 |
Correct |
2 ms |
6612 KB |
Output is correct |
6 |
Correct |
44 ms |
8436 KB |
Output is correct |
7 |
Correct |
62 ms |
8540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
164 ms |
9456 KB |
Output is correct |
2 |
Correct |
165 ms |
9468 KB |
Output is correct |
3 |
Correct |
285 ms |
12160 KB |
Output is correct |
4 |
Correct |
230 ms |
12940 KB |
Output is correct |
5 |
Correct |
110 ms |
9700 KB |
Output is correct |
6 |
Correct |
159 ms |
12616 KB |
Output is correct |
7 |
Correct |
175 ms |
12692 KB |
Output is correct |
8 |
Correct |
133 ms |
9484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6484 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
3 ms |
6484 KB |
Output is correct |
4 |
Correct |
3 ms |
6612 KB |
Output is correct |
5 |
Correct |
2 ms |
6612 KB |
Output is correct |
6 |
Correct |
3 ms |
6600 KB |
Output is correct |
7 |
Correct |
2 ms |
6612 KB |
Output is correct |
8 |
Correct |
3 ms |
6484 KB |
Output is correct |
9 |
Correct |
3 ms |
6612 KB |
Output is correct |
10 |
Correct |
2 ms |
6484 KB |
Output is correct |
11 |
Correct |
3 ms |
6612 KB |
Output is correct |
12 |
Correct |
3 ms |
6612 KB |
Output is correct |
13 |
Correct |
3 ms |
6612 KB |
Output is correct |
14 |
Correct |
4 ms |
6612 KB |
Output is correct |
15 |
Correct |
3 ms |
6592 KB |
Output is correct |
16 |
Correct |
4 ms |
6612 KB |
Output is correct |
17 |
Correct |
3 ms |
6596 KB |
Output is correct |
18 |
Correct |
3 ms |
6612 KB |
Output is correct |
19 |
Correct |
4 ms |
6612 KB |
Output is correct |
20 |
Correct |
4 ms |
6608 KB |
Output is correct |
21 |
Correct |
4 ms |
6612 KB |
Output is correct |
22 |
Correct |
4 ms |
6612 KB |
Output is correct |
23 |
Correct |
4 ms |
6612 KB |
Output is correct |
24 |
Correct |
3 ms |
6604 KB |
Output is correct |
25 |
Correct |
3 ms |
6612 KB |
Output is correct |
26 |
Correct |
4 ms |
6612 KB |
Output is correct |
27 |
Correct |
316 ms |
12160 KB |
Output is correct |
28 |
Correct |
321 ms |
14072 KB |
Output is correct |
29 |
Correct |
310 ms |
14112 KB |
Output is correct |
30 |
Correct |
309 ms |
14136 KB |
Output is correct |
31 |
Correct |
170 ms |
14120 KB |
Output is correct |
32 |
Correct |
76 ms |
8004 KB |
Output is correct |
33 |
Correct |
81 ms |
8528 KB |
Output is correct |
34 |
Correct |
100 ms |
8484 KB |
Output is correct |
35 |
Correct |
43 ms |
8472 KB |
Output is correct |
36 |
Correct |
2 ms |
6612 KB |
Output is correct |
37 |
Correct |
44 ms |
8436 KB |
Output is correct |
38 |
Correct |
62 ms |
8540 KB |
Output is correct |
39 |
Correct |
164 ms |
9456 KB |
Output is correct |
40 |
Correct |
165 ms |
9468 KB |
Output is correct |
41 |
Correct |
285 ms |
12160 KB |
Output is correct |
42 |
Correct |
230 ms |
12940 KB |
Output is correct |
43 |
Correct |
110 ms |
9700 KB |
Output is correct |
44 |
Correct |
159 ms |
12616 KB |
Output is correct |
45 |
Correct |
175 ms |
12692 KB |
Output is correct |
46 |
Correct |
133 ms |
9484 KB |
Output is correct |
47 |
Correct |
160 ms |
10372 KB |
Output is correct |
48 |
Correct |
179 ms |
10432 KB |
Output is correct |
49 |
Correct |
344 ms |
14036 KB |
Output is correct |
50 |
Correct |
185 ms |
14052 KB |
Output is correct |
51 |
Correct |
146 ms |
12376 KB |
Output is correct |
52 |
Correct |
204 ms |
14208 KB |
Output is correct |
53 |
Correct |
210 ms |
14244 KB |
Output is correct |
54 |
Correct |
183 ms |
14948 KB |
Output is correct |
55 |
Correct |
277 ms |
14136 KB |
Output is correct |