This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |