#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (int)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) __builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 2e5 + 100;
struct st{
vector <int> seg;
void ass(int n) {
seg.assign(n * 4 + 100, 0);
}
void upd(int id, int l, int r, int p, int v) {
if (l > p || r < p)
return;
seg[id] = max(seg[id], v);
if (l == r)
return;
int m = (l + r) / 2;
upd(id * 2, l, m, p, v);
upd(id * 2 + 1, m + 1, r, p, v);
seg[id] = max(seg[id * 2], seg[id * 2 + 1]);
}
int get(int id, int l, int r, int u, int v) {
if (l > v || r < u || u > v)
return 0;
int m = (l + r) / 2;
if (l >= u && r <= v)
return seg[id];
return max(get(id * 2, l, m, u, v), get(id * 2 + 1, m + 1, r, u, v));
}
};
int a[N];
vector <int> val;
int find(int x) {
int p = lower_bound(all(val), x) - val.begin() + 1;
return p;
}
int pr[N], suff[N];
ll solve() {
int n, x;
cin >> n >> x;
for (int i = 1; i <= n; i++) {
cin >> a[i];
val.pb(a[i]);
}
uniquev(val);
st seg;
seg.ass(n);
for (int i = 1; i <= n; i++) {
int p = find(a[i]);
pr[i] = seg.get(1, 1, n, 1, p - 1) + 1;
seg.upd(1, 1, n, p, pr[i]);
}
seg.ass(n);
for (int i = n; i >= 1; i--) {
int p = find(a[i]);
suff[i] = seg.get(1, 1, n, p + 1, n) + 1;
seg.upd(1, 1, n, p, suff[i]);
}
int ans = 0;
for (int i = 1; i <= n; i++)
ans = max(ans, pr[i]);
seg.ass(n);
for (int i = 1; i < n; i++) {
int p = find(a[i]);
seg.upd(1, 1, n, p, pr[i]);
int nxt = find(a[i + 1] + x - 1);
ans = max(ans, seg.get(1, 1, n, 1, nxt - 1) + suff[i + 1]);
}
seg.ass(n);
for (int i = n; i > 1; i--) {
int p = find(a[i]);
seg.upd(1, 1, n, p, suff[i]);
int nxt = find(a[i - 1] - x + 1);
ans = max(ans, seg.get(1, 1, n, nxt, n) + pr[i - 1]);
}
return ans;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t = 1;
//cin >> t;
while (t--) {
//solve();
cout << solve() << '\n';
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2648 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2520 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2648 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2520 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2648 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2520 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
2 ms |
2392 KB |
Output is correct |
20 |
Correct |
2 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
2396 KB |
Output is correct |
22 |
Correct |
2 ms |
2392 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2516 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
2 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
384 ms |
8520 KB |
Output is correct |
2 |
Correct |
385 ms |
8824 KB |
Output is correct |
3 |
Correct |
378 ms |
8620 KB |
Output is correct |
4 |
Correct |
377 ms |
8780 KB |
Output is correct |
5 |
Correct |
182 ms |
7892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
4308 KB |
Output is correct |
2 |
Correct |
81 ms |
4120 KB |
Output is correct |
3 |
Correct |
81 ms |
4060 KB |
Output is correct |
4 |
Correct |
42 ms |
4052 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
46 ms |
4056 KB |
Output is correct |
7 |
Correct |
75 ms |
4284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
146 ms |
5584 KB |
Output is correct |
2 |
Correct |
146 ms |
5732 KB |
Output is correct |
3 |
Correct |
318 ms |
8772 KB |
Output is correct |
4 |
Correct |
164 ms |
7880 KB |
Output is correct |
5 |
Correct |
93 ms |
5328 KB |
Output is correct |
6 |
Correct |
171 ms |
7808 KB |
Output is correct |
7 |
Correct |
185 ms |
8308 KB |
Output is correct |
8 |
Correct |
125 ms |
5592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2648 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
2 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2520 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
2 ms |
2392 KB |
Output is correct |
20 |
Correct |
2 ms |
2396 KB |
Output is correct |
21 |
Correct |
2 ms |
2396 KB |
Output is correct |
22 |
Correct |
2 ms |
2392 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2516 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
2 ms |
2396 KB |
Output is correct |
27 |
Correct |
384 ms |
8520 KB |
Output is correct |
28 |
Correct |
385 ms |
8824 KB |
Output is correct |
29 |
Correct |
378 ms |
8620 KB |
Output is correct |
30 |
Correct |
377 ms |
8780 KB |
Output is correct |
31 |
Correct |
182 ms |
7892 KB |
Output is correct |
32 |
Correct |
83 ms |
4308 KB |
Output is correct |
33 |
Correct |
81 ms |
4120 KB |
Output is correct |
34 |
Correct |
81 ms |
4060 KB |
Output is correct |
35 |
Correct |
42 ms |
4052 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
46 ms |
4056 KB |
Output is correct |
38 |
Correct |
75 ms |
4284 KB |
Output is correct |
39 |
Correct |
146 ms |
5584 KB |
Output is correct |
40 |
Correct |
146 ms |
5732 KB |
Output is correct |
41 |
Correct |
318 ms |
8772 KB |
Output is correct |
42 |
Correct |
164 ms |
7880 KB |
Output is correct |
43 |
Correct |
93 ms |
5328 KB |
Output is correct |
44 |
Correct |
171 ms |
7808 KB |
Output is correct |
45 |
Correct |
185 ms |
8308 KB |
Output is correct |
46 |
Correct |
125 ms |
5592 KB |
Output is correct |
47 |
Correct |
180 ms |
5728 KB |
Output is correct |
48 |
Correct |
174 ms |
5540 KB |
Output is correct |
49 |
Correct |
374 ms |
8652 KB |
Output is correct |
50 |
Correct |
192 ms |
7884 KB |
Output is correct |
51 |
Correct |
154 ms |
6604 KB |
Output is correct |
52 |
Correct |
252 ms |
7888 KB |
Output is correct |
53 |
Correct |
176 ms |
8028 KB |
Output is correct |
54 |
Correct |
176 ms |
8664 KB |
Output is correct |
55 |
Correct |
315 ms |
8808 KB |
Output is correct |