이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
void maxSelf(int &x, int y) {
if (x < y) {
x = y;
}
}
struct ST {
int n;
vector<int> t;
ST(int N) : n(N) {
int dim = 1;
while (dim < n) {
dim *= 2;
}
t.resize(dim * 2);
}
void reset() {
for (int &it : t) {
it = 0;
}
}
void update(int x, int lx, int rx, int pos, int v) {
if (lx == rx) {
t[x] = v;
return;
}
int mid = (lx + rx) / 2;
if (pos <= mid) {
update(x * 2, lx, mid, pos, v);
} else {
update(x * 2 + 1, mid + 1, rx, pos, v);
}
t[x] = max(t[x * 2], t[x * 2 + 1]);
}
void update(int pos, int v) {
update(1, 0, n - 1, pos, v);
}
int query(int x, int lx, int rx, int st, int dr) {
if (st <= lx && rx <= dr) {
return t[x];
}
int mid = (lx + rx) / 2, ans = 0;
if (st <= mid) {
maxSelf(ans, query(x * 2, lx, mid, st, dr));
}
if (mid < dr) {
maxSelf(ans, query(x * 2 + 1, mid + 1, rx, st, dr));
}
return ans;
}
int query(int st, int dr) {
if (dr < st) {
return 0;
}
return query(1, 0, n - 1, st, dr);
}
};
void testCase() {
int n, x;
cin >> n >> x;
vector<int> a(n);
for (int &x : a) {
cin >> x;
}
vector<int> v = a;
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
int m = v.size();
ST t(m);
vector<int> dp(n);
auto getIndex = [&](const int &val) -> int {
return distance(v.begin(), lower_bound(v.begin(), v.end(), val));
};
for (int i = n - 1; i >= 0; --i) {
int index = getIndex(a[i]);
dp[i] = t.query(index + 1, m - 1) + 1;
t.update(index, dp[i]);
}
t.reset();
int ans = 0;
for (int i = 0; i < n; ++i) {
maxSelf(ans, t.query(0, getIndex(a[i] + x) - 1) + dp[i]);
a[i] = getIndex(a[i]);
int best = t.query(0, a[i] - 1) + 1;
t.update(a[i], best);
}
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int tests = 1;
for (int tc = 0; tc < tests; ++tc) {
testCase();
}
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... |