#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 17;
int n, c, a[N], b[N], ans;
int st[4 * N], fl[N], fr[N];
void build (int id, int l, int r)
{
st[id] = 0;
if (l == r)
{
return;
}
int mid = l + r >> 1;
build (id * 2, l, mid);
build (id * 2 + 1, mid + 1, r);
}
void update (int id, int l, int r, int u, long long z)
{
if (l == r)
{
st[id] = z;
return;
}
int mid = l + r >> 1;
if (u <= mid)
{
update (id * 2, l, mid, u, z);
}
else
{
update (id * 2 + 1, mid + 1, r, u, z);
}
st[id] = max (st[id * 2], st[id * 2 + 1]);
}
int get (int id, int l, int r, int u, int v)
{
if (u > r || l > v)
{
return 0;
}
if (u <= l && v >= r)
{
return st[id];
}
int mid = l + r >> 1;
return max (get (id * 2, l, mid, u, v), get (id * 2 + 1, mid + 1, r, u, v));
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> c;
for (int i = 1; i <= n; ++i)
{
cin >> a[i];
b[i] = a[i];
}
sort (b + 1, b + n + 1);
for (int i = 1; i <= n; ++i)
{
int id = lower_bound (b + 1, b + n + 1, a[i]) - b;
fr[i] = get (1, 1, n, 1, id - 1) + 1;
update (1, 1, n, id, fr[i]);
}
build (1, 1, n);
for (int i = n; i >= 1; --i)
{
int id = lower_bound (b + 1, b + n + 1, a[i]) - b;
fl[i] = get (1, 1, n, 1, n - id) + 1;
update (1, 1, n, n - id + 1, fl[i]);
}
build (1, 1, n);
for (int i = 1; i <= n; ++i)
{
int id = lower_bound (b + 1, b + n + 1, a[i] + c) - b;
ans = max (ans, get (1, 1, n, 1, id - 1) + fl[i]);
id = lower_bound (b + 1, b + n + 1, a[i]) - b;
update (1, 1, n, id, fr[i]);
}
cout << ans;
}
Compilation message
glo.cpp: In function 'void build(int, int, int)':
glo.cpp:13:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
13 | int mid = l + r >> 1;
| ~~^~~
glo.cpp: In function 'void update(int, int, int, int, long long int)':
glo.cpp:24:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
24 | int mid = l + r >> 1;
| ~~^~~
glo.cpp: In function 'int get(int, int, int, int, int)':
glo.cpp:45:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
45 | int mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
464 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 |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
464 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 |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
352 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
464 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 |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
352 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
360 KB |
Output is correct |
19 |
Correct |
1 ms |
360 KB |
Output is correct |
20 |
Correct |
1 ms |
356 KB |
Output is correct |
21 |
Correct |
1 ms |
356 KB |
Output is correct |
22 |
Correct |
1 ms |
356 KB |
Output is correct |
23 |
Correct |
1 ms |
516 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
278 ms |
7328 KB |
Output is correct |
2 |
Correct |
284 ms |
7304 KB |
Output is correct |
3 |
Correct |
277 ms |
7504 KB |
Output is correct |
4 |
Correct |
288 ms |
7504 KB |
Output is correct |
5 |
Correct |
130 ms |
6752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
2240 KB |
Output is correct |
2 |
Correct |
63 ms |
2112 KB |
Output is correct |
3 |
Correct |
56 ms |
2036 KB |
Output is correct |
4 |
Correct |
30 ms |
1880 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
34 ms |
2040 KB |
Output is correct |
7 |
Correct |
47 ms |
2128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
4016 KB |
Output is correct |
2 |
Correct |
109 ms |
3952 KB |
Output is correct |
3 |
Correct |
232 ms |
7324 KB |
Output is correct |
4 |
Correct |
107 ms |
6740 KB |
Output is correct |
5 |
Correct |
65 ms |
3408 KB |
Output is correct |
6 |
Correct |
115 ms |
6560 KB |
Output is correct |
7 |
Correct |
117 ms |
7268 KB |
Output is correct |
8 |
Correct |
88 ms |
3920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
464 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 |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
352 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
360 KB |
Output is correct |
19 |
Correct |
1 ms |
360 KB |
Output is correct |
20 |
Correct |
1 ms |
356 KB |
Output is correct |
21 |
Correct |
1 ms |
356 KB |
Output is correct |
22 |
Correct |
1 ms |
356 KB |
Output is correct |
23 |
Correct |
1 ms |
516 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
278 ms |
7328 KB |
Output is correct |
28 |
Correct |
284 ms |
7304 KB |
Output is correct |
29 |
Correct |
277 ms |
7504 KB |
Output is correct |
30 |
Correct |
288 ms |
7504 KB |
Output is correct |
31 |
Correct |
130 ms |
6752 KB |
Output is correct |
32 |
Correct |
57 ms |
2240 KB |
Output is correct |
33 |
Correct |
63 ms |
2112 KB |
Output is correct |
34 |
Correct |
56 ms |
2036 KB |
Output is correct |
35 |
Correct |
30 ms |
1880 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
34 ms |
2040 KB |
Output is correct |
38 |
Correct |
47 ms |
2128 KB |
Output is correct |
39 |
Correct |
108 ms |
4016 KB |
Output is correct |
40 |
Correct |
109 ms |
3952 KB |
Output is correct |
41 |
Correct |
232 ms |
7324 KB |
Output is correct |
42 |
Correct |
107 ms |
6740 KB |
Output is correct |
43 |
Correct |
65 ms |
3408 KB |
Output is correct |
44 |
Correct |
115 ms |
6560 KB |
Output is correct |
45 |
Correct |
117 ms |
7268 KB |
Output is correct |
46 |
Correct |
88 ms |
3920 KB |
Output is correct |
47 |
Correct |
125 ms |
3924 KB |
Output is correct |
48 |
Correct |
123 ms |
3964 KB |
Output is correct |
49 |
Correct |
271 ms |
7732 KB |
Output is correct |
50 |
Correct |
123 ms |
6744 KB |
Output is correct |
51 |
Correct |
112 ms |
5716 KB |
Output is correct |
52 |
Correct |
167 ms |
6836 KB |
Output is correct |
53 |
Correct |
137 ms |
6744 KB |
Output is correct |
54 |
Correct |
130 ms |
7504 KB |
Output is correct |
55 |
Correct |
220 ms |
7504 KB |
Output is correct |