#include <bits/stdc++.h>
using namespace std;
#define int long long
#define INF
struct FenwickTree {
int n;
vector<int> t;
FenwickTree(int n) : n(n), t(n) {}
int lsb(int x) {
return x & -x;
}
void update(int p, int x) {
while (p <= n) {
t[p] = max(t[p], x);
p += lsb(p);
}
}
int query(int p) {
int ans = 0;
while (p > 0) {
ans = max(ans, t[p]);
p -= lsb(p);
}
return ans;
}
};
void solve() {
int n, d;
cin >> n >> d;
vector<int> t(n), a;
for (int i = 0; i < n; i++) {
cin >> t[i];
a.push_back(t[i]);
a.push_back(t[i] - d);
}
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
// for (int x : a) {
// cout << x << "\n";
// }
vector<pair<int, int>> v(n);
for (int i = 0; i < n; i++) {
v[i].first = lower_bound(a.begin(), a.end(), t[i]) - a.begin() + 1;
v[i].second = lower_bound(a.begin(), a.end(), t[i] - d) - a.begin() + 1;
// cout << t[i] << " " << v[i].first << " " << v[i].second << "\n";
}
int ans = 1;
vector<vector<int>> dp(n, vector<int>(2));
vector<FenwickTree> ft(2, FenwickTree(2 * n + 1));
for (int i = 0; i < n; i++) {
dp[i][0] = ft[0].query(v[i].first - 1) + 1;
dp[i][1] = max(ft[0].query(v[i].first - 1), ft[1].query(v[i].second - 1)) + 1;
ft[0].update(v[i].first, dp[i][0]);
ft[1].update(v[i].first, dp[i][1]);
ans = max(ans, max(dp[i][0], dp[i][1]));
}
cout << ans;
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
solve();
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... |