//#pragma GCC optimize("Ofast,O2,O3,unroll-loops")
//#pragma GCC target("avx2")
#include <bits/stdc++.h>
using namespace std;
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << "[" << H << "]";
debug_out(T...);
}
#ifdef dddxxz
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
#define SZ(s) ((int)s.size())
#define all(x) (x).begin(), (x).end()
#define lla(x) (x).rbegin(), (x).rend()
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)
clock_t startTime;
double getCurrentTime() {
return (double) (clock() - startTime) / CLOCKS_PER_SEC;
}
#define MP make_pair
typedef long long ll;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const double eps = 0.000001;
const int MOD = 1e9 + 7;
const int INF = 1000000101;
const long long LLINF = 1223372000000000555;
const int N = 1e6 + 3e2;
const int M = 1248;
struct SegTree{
vector<int> tree;
int size = 1;
SegTree(int n){
size = n;
tree.assign(4 * n + 5, 0);
}
void update(int v, int tl, int tr, int pos, int val, bool flag){
if (tl == tr){
if (flag) tree[v] = max(tree[v], val); else
tree[v] = val;
return;
}
int tm = (tl + tr) >> 1;
if (pos <= tm) update(v << 1, tl, tm, pos, val, flag); else
update(v << 1 | 1, tm + 1, tr, pos, val, flag);
tree[v] = max(tree[v << 1], tree[v << 1 | 1]);
}
void update(int pos, int val, bool flag){
update(1, 0, size - 1, pos, val, flag);
}
int get(int v, int tl, int tr, int l, int r){
if (l <= tl && tr <= r) return tree[v];
if (tl > r || tr < l) return 0;
int tm = (tl + tr) >> 1;
return max(get(v << 1, tl, tm, l, r), get(v << 1 | 1, tm + 1, tr, l, r));
}
int get(int l, int r){
return get(1, 0, size - 1, l, r);
}
};
void solve(int TC) {
int n, lim;
cin >> n >> lim;
vector<int> a(n);
for (int i = 0; i < n; i++){
cin >> a[i];
}
map<int, int> mp;
for (int i : a) mp[i] = 0;
int foo = 0;
for (auto &now : mp) now.second = ++foo;
SegTree st1(n), st2(n);
vector<int> dp1(n, 0), dp2(n, 0);
for (int i = 0; i < n; i++){
int x = mp[a[i]];
dp1[i] = st1.get(0, x - 1) + 1;
st1.update(x, dp1[i], true);
}
for (int i = n - 1; i >= 0; i--){
int x = mp[a[i]];
dp2[i] = st2.get(x + 1, n - 1) + 1;
st2.update(x, dp2[i], true);
}
vector<pair<int, int>> b;
for (int i = 0; i < n; i++) b.emplace_back(a[i], i);
sort(all(b));
vector<int> pos(n);
for (int i = 0; i < n; i++) pos[b[i].second] = i;
SegTree st(n);
for (int i = 0; i < n; i++) st.update(pos[i], dp2[i], false);
int ans = 0;
for (int i = 0; i < n; i++){
st.update(pos[i], 0, false);
int p = upper_bound(all(b), MP(a[i] - lim, INF)) - b.begin();
if (p < n){
int val = st.get(p, n - 1);
ans = max(ans, dp1[i] + val);
}
}
cout << ans << endl;
}
int main() {
startTime = clock();
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#ifdef dddxxz
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int TC = 1;
//cin >> TC;
for (int test = 1; test <= TC; test++) {
//debug(test);
//cout << "Case #" << test << ": ";
solve(test);
}
#ifdef dddxxz
cerr << endl << "Time: " << int(getCurrentTime() * 1000) << " ms" << endl;
#endif
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
0 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
0 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
0 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
599 ms |
23924 KB |
Output is correct |
2 |
Correct |
556 ms |
23828 KB |
Output is correct |
3 |
Correct |
584 ms |
23780 KB |
Output is correct |
4 |
Correct |
600 ms |
23868 KB |
Output is correct |
5 |
Correct |
221 ms |
19252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
6204 KB |
Output is correct |
2 |
Correct |
91 ms |
6588 KB |
Output is correct |
3 |
Correct |
96 ms |
6680 KB |
Output is correct |
4 |
Correct |
49 ms |
5216 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
49 ms |
5228 KB |
Output is correct |
7 |
Correct |
73 ms |
6620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
198 ms |
11996 KB |
Output is correct |
2 |
Correct |
232 ms |
11972 KB |
Output is correct |
3 |
Correct |
512 ms |
23872 KB |
Output is correct |
4 |
Correct |
216 ms |
19128 KB |
Output is correct |
5 |
Correct |
106 ms |
11996 KB |
Output is correct |
6 |
Correct |
200 ms |
22536 KB |
Output is correct |
7 |
Correct |
210 ms |
22556 KB |
Output is correct |
8 |
Correct |
137 ms |
11968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
324 KB |
Output is correct |
8 |
Correct |
1 ms |
320 KB |
Output is correct |
9 |
Correct |
0 ms |
320 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
324 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
2 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
320 KB |
Output is correct |
27 |
Correct |
599 ms |
23924 KB |
Output is correct |
28 |
Correct |
556 ms |
23828 KB |
Output is correct |
29 |
Correct |
584 ms |
23780 KB |
Output is correct |
30 |
Correct |
600 ms |
23868 KB |
Output is correct |
31 |
Correct |
221 ms |
19252 KB |
Output is correct |
32 |
Correct |
101 ms |
6204 KB |
Output is correct |
33 |
Correct |
91 ms |
6588 KB |
Output is correct |
34 |
Correct |
96 ms |
6680 KB |
Output is correct |
35 |
Correct |
49 ms |
5216 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
49 ms |
5228 KB |
Output is correct |
38 |
Correct |
73 ms |
6620 KB |
Output is correct |
39 |
Correct |
198 ms |
11996 KB |
Output is correct |
40 |
Correct |
232 ms |
11972 KB |
Output is correct |
41 |
Correct |
512 ms |
23872 KB |
Output is correct |
42 |
Correct |
216 ms |
19128 KB |
Output is correct |
43 |
Correct |
106 ms |
11996 KB |
Output is correct |
44 |
Correct |
200 ms |
22536 KB |
Output is correct |
45 |
Correct |
210 ms |
22556 KB |
Output is correct |
46 |
Correct |
137 ms |
11968 KB |
Output is correct |
47 |
Correct |
215 ms |
12992 KB |
Output is correct |
48 |
Correct |
274 ms |
12936 KB |
Output is correct |
49 |
Correct |
531 ms |
25748 KB |
Output is correct |
50 |
Correct |
217 ms |
20292 KB |
Output is correct |
51 |
Correct |
152 ms |
16452 KB |
Output is correct |
52 |
Correct |
273 ms |
24964 KB |
Output is correct |
53 |
Correct |
206 ms |
25016 KB |
Output is correct |
54 |
Correct |
227 ms |
25776 KB |
Output is correct |
55 |
Correct |
353 ms |
25768 KB |
Output is correct |