#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e18
#define f first
#define s second
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
int n, x;
const int N = 2e5 + 69;
int seg[8 * N][2], a[N], dp[N][2], b[N];
map <int, int> mp;
set <int> st;
void upd(int l, int r, int pos, int qp, int v, int i){
seg[pos][i] = max(seg[pos][i], v);
if (l == r){
return;
}
int mid = (l + r)/2;
if (qp <= mid) upd(l, mid, pos*2, qp, v, i);
else upd(mid + 1, r, pos*2 + 1, qp, v, i);
}
int query(int l, int r, int pos, int ql, int qr, int i){
if (l >= ql && r <= qr) return seg[pos][i];
else if (l > qr || r < ql) return 0;
int mid = (l + r)/2;
return max(query(l, mid, pos*2, ql, qr, i), query(mid + 1, r, pos*2 + 1, ql, qr, i));
}
void Solve()
{
cin >> n >> x;
for (int i = 1; i <= n; i++){
cin >> a[i];
st.insert(a[i]);
st.insert(a[i] + x);
}
int ptr = 0;
for (auto x : st){
mp[x] = ++ptr;
}
for (int i = 1; i <= n; i++){
b[i] = a[i];
a[i] = mp[a[i]];
}
int ans = 0;
for (int i = 1; i <= n; i++){
dp[i][0] = query(1, 2 * n, 1, 1, a[i] - 1, 0) + 1;
dp[i][1] = query(1, 2 * n, 1, 1, a[i] - 1, 1) + 1;
dp[i][1] = max(dp[i][1], query(1, 2 * n, 1, 1, mp[b[i] + x] - 1, 0) + 1);
upd(1, 2 * n, 1, a[i], dp[i][0], 0);
upd(1, 2 * n, 1, a[i], dp[i][1], 1);
ans = max(ans, dp[i][0]);
ans = max(ans, dp[i][1]);
}
cout << ans << "\n";
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
461 ms |
38568 KB |
Output is correct |
2 |
Correct |
465 ms |
38656 KB |
Output is correct |
3 |
Correct |
486 ms |
38568 KB |
Output is correct |
4 |
Correct |
443 ms |
38668 KB |
Output is correct |
5 |
Correct |
208 ms |
22752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
17432 KB |
Output is correct |
2 |
Correct |
126 ms |
17356 KB |
Output is correct |
3 |
Correct |
110 ms |
17368 KB |
Output is correct |
4 |
Correct |
44 ms |
9736 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
48 ms |
9676 KB |
Output is correct |
7 |
Correct |
94 ms |
14924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
236 ms |
30448 KB |
Output is correct |
2 |
Correct |
219 ms |
30380 KB |
Output is correct |
3 |
Correct |
611 ms |
60580 KB |
Output is correct |
4 |
Correct |
225 ms |
33696 KB |
Output is correct |
5 |
Correct |
154 ms |
30072 KB |
Output is correct |
6 |
Correct |
297 ms |
57280 KB |
Output is correct |
7 |
Correct |
300 ms |
58036 KB |
Output is correct |
8 |
Correct |
197 ms |
30568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
328 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
328 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
596 KB |
Output is correct |
21 |
Correct |
2 ms |
596 KB |
Output is correct |
22 |
Correct |
2 ms |
468 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
468 KB |
Output is correct |
25 |
Correct |
2 ms |
468 KB |
Output is correct |
26 |
Correct |
1 ms |
468 KB |
Output is correct |
27 |
Correct |
461 ms |
38568 KB |
Output is correct |
28 |
Correct |
465 ms |
38656 KB |
Output is correct |
29 |
Correct |
486 ms |
38568 KB |
Output is correct |
30 |
Correct |
443 ms |
38668 KB |
Output is correct |
31 |
Correct |
208 ms |
22752 KB |
Output is correct |
32 |
Correct |
97 ms |
17432 KB |
Output is correct |
33 |
Correct |
126 ms |
17356 KB |
Output is correct |
34 |
Correct |
110 ms |
17368 KB |
Output is correct |
35 |
Correct |
44 ms |
9736 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
48 ms |
9676 KB |
Output is correct |
38 |
Correct |
94 ms |
14924 KB |
Output is correct |
39 |
Correct |
236 ms |
30448 KB |
Output is correct |
40 |
Correct |
219 ms |
30380 KB |
Output is correct |
41 |
Correct |
611 ms |
60580 KB |
Output is correct |
42 |
Correct |
225 ms |
33696 KB |
Output is correct |
43 |
Correct |
154 ms |
30072 KB |
Output is correct |
44 |
Correct |
297 ms |
57280 KB |
Output is correct |
45 |
Correct |
300 ms |
58036 KB |
Output is correct |
46 |
Correct |
197 ms |
30568 KB |
Output is correct |
47 |
Correct |
266 ms |
34552 KB |
Output is correct |
48 |
Correct |
266 ms |
34512 KB |
Output is correct |
49 |
Correct |
727 ms |
68696 KB |
Output is correct |
50 |
Correct |
214 ms |
37808 KB |
Output is correct |
51 |
Correct |
167 ms |
27780 KB |
Output is correct |
52 |
Correct |
235 ms |
38092 KB |
Output is correct |
53 |
Correct |
302 ms |
59936 KB |
Output is correct |
54 |
Correct |
309 ms |
60564 KB |
Output is correct |
55 |
Correct |
396 ms |
58864 KB |
Output is correct |