// In the name of Allah
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define all(x) (x).begin(),(x).end()
#define len(x) ((ll) (x).size())
#define F first
#define S second
#define pb push_back
#define sep ' '
#define endl '\n'
#define Mp make_pair
#define debug(x) cerr << #x << ": " << x << endl;
#define kill(x) cout << x << '\n', exit(0);
#define set_dec(x) cout << fixed << setprecision(x);
#define file_io(x,y) freopen(x, "r", stdin); freopen(y, "w", stdout);
int n; ll x;
const int maxn = 1e6 + 5;
const ll oo = 1e18;
ll a[maxn]; ll res1[maxn], res2[maxn];
ll ans = 0;
vector<ll> arr;
ll t[4 * maxn]; vector<pll> vc[maxn];
int GI(ll val) {
return lower_bound(all(arr), val) - arr.begin();
}
void set_val(int v, int tl, int tr, int i, ll x) {
if (i >= tr || i < tl) return ;
if (tr - tl == 1) {
t[v] = x;
return ;
}
int mid = (tl + tr) / 2;
set_val(2 * v + 1, tl, mid, i, x); set_val(2 * v + 2, mid, tr, i, x);
t[v] = max(t[2 * v + 1], t[2 * v + 2]);
}
ll get_max(int v, int tl, int tr, int l, int r) {
l = max(l, tl); r = min(r, tr);
if (l >= tr || r <= tl) return 0;
if (l == tl && r == tr) return t[v];
int mid = (tl + tr) / 2;
return max(get_max(2 * v + 1, tl, mid, l, r), get_max(2 * v + 2, mid, tr, l, r));
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> x;
for (int i = 1; i <= n; i++) {
cin >> a[i];
arr.pb(a[i]); arr.pb(a[i] - x + 1); arr.pb(a[i] + x - 1);
}
arr.pb(-oo); arr.pb(oo);
sort(all(arr)); arr.resize(unique(all(arr)) - arr.begin());
// Part 1
for (int i = 0; i <= n + 1; i++) vc[i].clear();
fill(t, t + 4 * len(arr), 0);
reverse(a + 1, a + (n + 1));
for (int i = 1; i <= n; i++) a[i] = -a[i];
fill(res2, res2 + (n + 2), oo);
res2[0] = -oo;
for (int i = 1; i <= n; i++) {
int j = lower_bound(res2, res2 + (n + 1), a[i]) - res2;
if (a[i] > res2[j - 1]) {
res2[j] = a[i];
int jx = GI(-res2[j]);
vc[i].pb(Mp(get_max(0, 0, len(arr), jx, jx + 1), -res2[j]));
set_val(0, 0, len(arr), GI(-res2[j]), j);
}
}
reverse(a + 1, a + (n + 1));
for (int i = 1; i <= n; i++) a[i] = -a[i];
fill(res1, res1 + (n + 2), oo);
res1[0] = -oo;
for (int i = 1; i <= n; i++) {
int j = lower_bound(res1, res1 + (n + 1), a[i]) - res1;
if (a[i] > res1[j - 1]) {
res1[j] = a[i];
for (auto f : vc[n - i + 1]) {
set_val(0, 0, len(arr), GI(f.S), f.F);
}
int j1 = GI(res1[j] - x + 1), j2 = GI(oo);
ans = max(ans, j + get_max(0, 0, len(arr), j1, j2));
}
}
for (int i = 0; i <= n; i++) {
if (res1[i] < oo) ans = max(ans, 0ll + i);
}
// Part 2
for (int i = 0; i <= n + 1; i++) vc[i].clear();
fill(t, t + 4 * len(arr), 0);
fill(res1, res1 + (n + 2), oo);
res1[0] = -oo;
for (int i = 1; i <= n; i++) {
int j = lower_bound(res1, res1 + (n + 1), a[i]) - res1;
if (a[i] > res1[j - 1]) {
res1[j] = a[i];
int jx = GI(res1[j]);
vc[i].pb(Mp(get_max(0, 0, len(arr), jx, jx + 1), res1[j]));
set_val(0, 0, len(arr), GI(res1[j]), j);
}
}
reverse(a + 1, a + (n + 1));
for (int i = 1; i <= n; i++) a[i] = -a[i];
fill(res2, res2 + (n + 2), oo);
res2[0] = -oo;
for (int i = 1; i <= n; i++) {
int j = lower_bound(res2, res2 + (n + 1), a[i]) - res2;
if (a[i] > res2[j - 1]) {
res2[j] = a[i];
for (auto f : vc[n - i + 1]) {
set_val(0, 0, len(arr), GI(f.S), f.F);
}
int j1 = GI(-oo) + 1, j2 = GI(-res2[j] + x - 1) + 1;
ans = max(ans, j + get_max(0, 0, len(arr), j1, j2));
}
}
reverse(a + 1, a + (n + 1));
for (int i = 1; i <= n; i++) a[i] = -a[i];
for (int i = 0; i <= n; i++) {
if (res2[i] < oo) ans = max(ans, 0ll + i);
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
15 ms |
23724 KB |
Output is correct |
3 |
Correct |
13 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23788 KB |
Output is correct |
5 |
Correct |
12 ms |
23832 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23828 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
13 ms |
23828 KB |
Output is correct |
11 |
Correct |
12 ms |
23848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
15 ms |
23724 KB |
Output is correct |
3 |
Correct |
13 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23788 KB |
Output is correct |
5 |
Correct |
12 ms |
23832 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23828 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
13 ms |
23828 KB |
Output is correct |
11 |
Correct |
12 ms |
23848 KB |
Output is correct |
12 |
Correct |
13 ms |
23848 KB |
Output is correct |
13 |
Correct |
13 ms |
23800 KB |
Output is correct |
14 |
Correct |
13 ms |
23764 KB |
Output is correct |
15 |
Correct |
14 ms |
23852 KB |
Output is correct |
16 |
Correct |
13 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23896 KB |
Output is correct |
18 |
Correct |
13 ms |
23764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
15 ms |
23724 KB |
Output is correct |
3 |
Correct |
13 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23788 KB |
Output is correct |
5 |
Correct |
12 ms |
23832 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23828 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
13 ms |
23828 KB |
Output is correct |
11 |
Correct |
12 ms |
23848 KB |
Output is correct |
12 |
Correct |
13 ms |
23848 KB |
Output is correct |
13 |
Correct |
13 ms |
23800 KB |
Output is correct |
14 |
Correct |
13 ms |
23764 KB |
Output is correct |
15 |
Correct |
14 ms |
23852 KB |
Output is correct |
16 |
Correct |
13 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23896 KB |
Output is correct |
18 |
Correct |
13 ms |
23764 KB |
Output is correct |
19 |
Correct |
15 ms |
23956 KB |
Output is correct |
20 |
Correct |
15 ms |
23956 KB |
Output is correct |
21 |
Correct |
14 ms |
23960 KB |
Output is correct |
22 |
Correct |
15 ms |
23960 KB |
Output is correct |
23 |
Correct |
17 ms |
23952 KB |
Output is correct |
24 |
Correct |
18 ms |
23916 KB |
Output is correct |
25 |
Correct |
15 ms |
23892 KB |
Output is correct |
26 |
Correct |
18 ms |
23892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
972 ms |
60140 KB |
Output is correct |
2 |
Correct |
1030 ms |
60256 KB |
Output is correct |
3 |
Correct |
949 ms |
60180 KB |
Output is correct |
4 |
Correct |
955 ms |
60168 KB |
Output is correct |
5 |
Correct |
467 ms |
49976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
207 ms |
32912 KB |
Output is correct |
2 |
Correct |
202 ms |
32816 KB |
Output is correct |
3 |
Correct |
226 ms |
32820 KB |
Output is correct |
4 |
Correct |
117 ms |
30360 KB |
Output is correct |
5 |
Correct |
15 ms |
23764 KB |
Output is correct |
6 |
Correct |
115 ms |
28748 KB |
Output is correct |
7 |
Correct |
164 ms |
31876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
404 ms |
41916 KB |
Output is correct |
2 |
Correct |
421 ms |
41988 KB |
Output is correct |
3 |
Correct |
894 ms |
60136 KB |
Output is correct |
4 |
Correct |
481 ms |
49964 KB |
Output is correct |
5 |
Correct |
289 ms |
41528 KB |
Output is correct |
6 |
Correct |
493 ms |
57736 KB |
Output is correct |
7 |
Correct |
491 ms |
58428 KB |
Output is correct |
8 |
Correct |
321 ms |
42020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
15 ms |
23724 KB |
Output is correct |
3 |
Correct |
13 ms |
23764 KB |
Output is correct |
4 |
Correct |
12 ms |
23788 KB |
Output is correct |
5 |
Correct |
12 ms |
23832 KB |
Output is correct |
6 |
Correct |
12 ms |
23764 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
12 ms |
23828 KB |
Output is correct |
9 |
Correct |
12 ms |
23764 KB |
Output is correct |
10 |
Correct |
13 ms |
23828 KB |
Output is correct |
11 |
Correct |
12 ms |
23848 KB |
Output is correct |
12 |
Correct |
13 ms |
23848 KB |
Output is correct |
13 |
Correct |
13 ms |
23800 KB |
Output is correct |
14 |
Correct |
13 ms |
23764 KB |
Output is correct |
15 |
Correct |
14 ms |
23852 KB |
Output is correct |
16 |
Correct |
13 ms |
23764 KB |
Output is correct |
17 |
Correct |
12 ms |
23896 KB |
Output is correct |
18 |
Correct |
13 ms |
23764 KB |
Output is correct |
19 |
Correct |
15 ms |
23956 KB |
Output is correct |
20 |
Correct |
15 ms |
23956 KB |
Output is correct |
21 |
Correct |
14 ms |
23960 KB |
Output is correct |
22 |
Correct |
15 ms |
23960 KB |
Output is correct |
23 |
Correct |
17 ms |
23952 KB |
Output is correct |
24 |
Correct |
18 ms |
23916 KB |
Output is correct |
25 |
Correct |
15 ms |
23892 KB |
Output is correct |
26 |
Correct |
18 ms |
23892 KB |
Output is correct |
27 |
Correct |
972 ms |
60140 KB |
Output is correct |
28 |
Correct |
1030 ms |
60256 KB |
Output is correct |
29 |
Correct |
949 ms |
60180 KB |
Output is correct |
30 |
Correct |
955 ms |
60168 KB |
Output is correct |
31 |
Correct |
467 ms |
49976 KB |
Output is correct |
32 |
Correct |
207 ms |
32912 KB |
Output is correct |
33 |
Correct |
202 ms |
32816 KB |
Output is correct |
34 |
Correct |
226 ms |
32820 KB |
Output is correct |
35 |
Correct |
117 ms |
30360 KB |
Output is correct |
36 |
Correct |
15 ms |
23764 KB |
Output is correct |
37 |
Correct |
115 ms |
28748 KB |
Output is correct |
38 |
Correct |
164 ms |
31876 KB |
Output is correct |
39 |
Correct |
404 ms |
41916 KB |
Output is correct |
40 |
Correct |
421 ms |
41988 KB |
Output is correct |
41 |
Correct |
894 ms |
60136 KB |
Output is correct |
42 |
Correct |
481 ms |
49964 KB |
Output is correct |
43 |
Correct |
289 ms |
41528 KB |
Output is correct |
44 |
Correct |
493 ms |
57736 KB |
Output is correct |
45 |
Correct |
491 ms |
58428 KB |
Output is correct |
46 |
Correct |
321 ms |
42020 KB |
Output is correct |
47 |
Correct |
433 ms |
41916 KB |
Output is correct |
48 |
Correct |
442 ms |
41972 KB |
Output is correct |
49 |
Correct |
975 ms |
60180 KB |
Output is correct |
50 |
Correct |
463 ms |
50024 KB |
Output is correct |
51 |
Correct |
400 ms |
42592 KB |
Output is correct |
52 |
Correct |
551 ms |
47160 KB |
Output is correct |
53 |
Correct |
516 ms |
59484 KB |
Output is correct |
54 |
Correct |
550 ms |
60224 KB |
Output is correct |
55 |
Correct |
703 ms |
56068 KB |
Output is correct |