이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define pii pair<int, int>
#define piii pair<int, pii>
#define pb push_back
#define ll long long
#define pll pair<ll, ll>
#define ui unsigned int
#define ull unsigned long long
#define f first
#define s second
#define oo 1000000000
using namespace std;
const int up = 3e5 + 5;
int tree[up << 2];
void update(int node, int l, int r, int pos, int val) {
if(l == r) {
tree[node] = val;
}else {
int mid = (l + r) >> 1;
if(pos <= mid) {
update(node << 1, l, mid, pos, val);
}else {
update((node << 1) | 1, mid + 1, r, pos, val);
}
tree[node] = max(tree[node << 1], tree[(node << 1) | 1]);
}
}
int Max(int node, int l, int r, int ql, int qr) {
if(ql > qr || l > qr || ql > r) return 0;
if(ql <= l && r <= qr) {
return tree[node];
}else {
int mid = (l + r) >> 1;
return max(Max(node << 1, l, mid, ql, qr), Max((node << 1) | 1, mid + 1, r, ql, qr));
}
}
bool cmp(pii p1, pii p2) {
if(p1.f == p2.f) return p1.s > p2.s;
else return p1.f < p2.f;
}
void solve() {
int n, d;
cin >> n >> d;
vector<pii>a(n);
vector<int>dp(n, 1);
for(int i = 0; i < n; ++i) {
cin >> a[i].f;
a[i].s = i;
}
sort(a.begin(), a.end());
set<pii>ranges;
vector<int>to_left(n);
for(int i = 0; i < n; ++i) {
int idx = a[i].s;
pii cur = {idx, idx + d};
auto itr = ranges.upper_bound(cur);
if(itr != ranges.end() && itr->f <= cur.s) {
cur.s = max(cur.s, itr->s);
ranges.erase(itr);
}
itr = ranges.upper_bound(cur);
if((int)ranges.size() > 0 && itr != ranges.begin()) {
--itr;
if(itr->s >= cur.f) {
cur.f = itr->f;
cur.s = max(itr->s, cur.s);
ranges.erase(itr);
}
}
to_left[idx] = cur.f;
ranges.insert(cur);
}
sort(a.begin(), a.end(), cmp);
int res = 0;
for(int i = 0; i < n; ++i) {
int idx = a[i].s;
dp[idx] = Max(1, 0, n - 1, to_left[idx], idx - 1) + 1;
update(1, 0, n - 1, idx, dp[idx]);
//cout << "for " << idx << " " << dp[idx] << '\n';
res = max(res, dp[idx]);
}
cout << res << '\n';
}
int main() {
ios_base::sync_with_stdio(false);
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... |