이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define ld long double
#define fi first
#define se second
int a[200005], st1[1000005], st2[1000005], id[200005];
set<int> s;
priority_queue<pair<int, int>> pq[200005];
void update1(int x, int tl, int tr, int pos) {
if (tl == tr) {
st1[x] = pq[tl].top().fi;
return;
}
int tm = (tl + tr) / 2;
if (pos <= tm) update1(2 * x, tl, tm, pos);
else update1(2 * x + 1, tm + 1, tr, pos);
st1[x] = max(st1[2 * x], st1[2 * x + 1]);
}
void update2(int x, int tl, int tr, int pos, int i) {
if (tl == tr) {
while (!pq[tl].empty() and pq[tl].top().se >= i) pq[tl].pop();
if (pq[tl].empty()) st1[x] = 0;
else st1[x] = pq[tl].top().fi;
return;
}
int tm = (tl + tr) / 2;
if (pos <= tm) update2(2 * x, tl, tm, pos, i);
else update2(2 * x + 1, tm + 1, tr, pos, i);
st1[x] = max(st1[2 * x], st1[2 * x + 1]);
}
void update3(int x, int tl, int tr, int pos, int val) {
if (tl == tr) {
st2[x] = max(st2[x], val);
return;
}
int tm = (tl + tr) / 2;
if (pos <= tm) update3(2 * x, tl, tm, pos, val);
else update3(2 * x + 1, tm + 1, tr, pos, val);
st2[x] = max(st2[2 * x], st2[2 * x + 1]);
}
int get1(int x, int l, int r, int tl, int tr) {
if (l > r) return 0;
if (l == tl and r == tr) return st1[x];
int tm = (tl + tr) / 2;
return max(get1(2 * x, l, min(r, tm), tl, tm), get1(2 * x + 1, max(l, tm + 1), r, tm + 1, tr));
}
int get2(int x, int l, int r, int tl, int tr) {
if (l > r) return 0;
if (l == tl and r == tr) return st2[x];
int tm = (tl + tr) / 2;
return max(get2(2 * x, l, min(r, tm), tl, tm), get2(2 * x + 1, max(l, tm + 1), r, tm + 1, tr));
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
int n, x, ans = 1;
cin >> n >> x;
for (int i = 1; i <= n; i++) {
cin >> a[i];
s.insert(a[i]);
}
int cn = 1;
for (auto u : s) id[cn] = u, cn++;
cn--;
for (int i = 1; i <= n; i++) {
int l = 1, r = cn;
while (l < r) {
int m = (l + r) / 2;
if (id[m] < a[i]) l = m + 1;
else r = m;
}
a[i] = l;
int val = get1(1, 1, l - 1, 1, cn) + 1;
pq[l].push({val, i});
update1(1, 1, cn, l);
}
for (int i = n; i >= 1; i--) {
update2(1, 1, cn, a[i], i);
int l = 0, r = cn;
while (l < r) {
int m = (l + r + 1) / 2;
if (id[m] < id[a[i]] + x) l = m;
else r = m - 1;
}
int tmp = get2(1, a[i] + 1, cn, 1, cn) + 1;
ans = max(ans, get1(1, 1, l, 1, cn) + tmp);
update3(1, 1, cn, a[i], tmp);
}
cout << ans << "\n";
}
# | 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... |