이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const int N = 3e5 + 100;
int a[N], f[N], IT[N << 2], perm[N], kount[N], rightMost[N], leftMost[N];
void Update(int k, int l, int r, int u, int v) {
if (l == r) { IT[k] = v; return; }
int m = l + r >> 1;
if (u <= m) Update(k << 1, l, m, u, v);
else Update(k << 1 | 1, m + 1, r, u, v);
IT[k] = max(IT[k << 1], IT[k << 1 | 1]);
}
int Query(int k, int l, int r, int u, int v) {
if (l > v || r < u) return 0;
if (u <= l && r <= v) return IT[k];
int m = (l + r) >> 1;
return max(Query(k << 1, l, m, u, v), Query(k << 1 | 1, m + 1, r, u, v));
}
int root(int u) { return (kount[u] < 0 ? u : kount[u] = root(kount[u])); }
void Union(int u, int v) {
// cerr << "Union" << u << ' ' << v << '\n';
u = root(u), v = root(v);
if (u == v) return;
int t = kount[u] + kount[v];
if (kount[u] > kount[v]) swap(u, v);
kount[u] = t;
kount[v] = u;
rightMost[u] = max(rightMost[u], rightMost[v]);
}
int main() {
ios::sync_with_stdio(false); cin.tie(NULL);
int n, d;
cin >> n >> d;
int ans = 0;
vector<int> Rank;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
perm[i] = i;
// Rank.push_back(a[i]);
}
// sort(Rank.begin(), Rank.end());
// for (int i = 1; i <= n; ++i) cout << lower_bound(Rank.begin(), Rank.end(), a[i]) - Rank.begin() << ' ';
sort(perm + 1, perm + n + 1, [](int x, int y) {
return a[x] != a[y] ? a[x] < a[y] : x > y;
});
set<int> obtacles;
obtacles.insert(0);
for (int i = 1; i <= n; ++i) kount[i] = -1, rightMost[i] = i;
for (int i = n; i > 0; --i) {
int id = perm[i];
leftMost[id] = *(--obtacles.lower_bound(id));
// cerr << "ID = " << id << ' ' << leftMost[id] << '\n';
if (id > 1 && a[id - 1] >= a[id]) Union(id - 1, id);
if (id != n && a[id + 1] > a[id]) Union(id, id + 1);
int r = root(id);
if (-kount[r] >= d) {
// cerr << "Enough D " << id << ' ' << -kount[r] << ' ' << rightMost[r] << '\n';
obtacles.insert(rightMost[r]);
}
}
for (int i = 1; i <= n; ++i) {
int id = perm[i];
int f = Query(1, 1, n, leftMost[id], id) + 1;
// cerr << "At " << i << ' ' << id << ' ' << leftMost[id] << ' ' << f << '\n';
ans = max(ans, f);
Update(1, 1, n, id, f);
}
cout << ans << '\n';
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'void Update(int, int, int, int, int)':
Main.cpp:11:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
11 | int m = l + r >> 1;
| ~~^~~
# | 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... |