This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
constexpr int inf = 1e9 + 7;
constexpr int R = 1 << 19;
int mx[R * 2];
void change(int pos, int val) {
pos += R;
mx[pos] = val;
for (pos /= 2; pos > 0; pos /= 2) {
mx[pos] = max(mx[pos * 2], mx[pos * 2 + 1]);
}
}
int get(int l, int r) {
l += R;
r += R;
int ans = -inf;
while (l < r) {
if (l & 1) {
ans = max(ans, mx[l++]);
}
if (r & 1) {
ans = max(ans, mx[--r]);
}
l >>= 1;
r >>= 1;
}
return ans;
}
void solve() {
int n, d;
cin >> n >> d;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vector<pair<int, int>> ord(n);
for (int i = 0; i < n; ++i) {
ord[i] = {a[i], -i};
}
sort(ord.begin(), ord.end());
vector<pair<int, int>> lr(n);
set<int> used, s;
for (auto [vl, pos] : ord) {
int i = -pos;
auto it = used.lower_bound(i);
if (it != used.end() && *it - i <= d && s.count(*it)) {
s.erase(*it);
}
if (it != used.begin()) {
it--;
if (i - *it > d) {
s.insert(i);
}
} else {
s.insert(i);
}
used.insert(i);
it = s.upper_bound(i);
if (it != s.begin()) {
it--;
lr[i] = {*it, i};
} else {
lr[i] = {0, i};
}
}
for (int i = 0; i < n; ++i) {
change(i, -inf);
}
for (auto [vl, pos] : ord) {
int i = -pos;
change(i, max(1, get(lr[i].first, lr[i].second) + 1));
}
cout << get(0, n) << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) {
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... |