이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#ifdef __LOCAL__
#include <debug_local.h>
#endif
using namespace std;
const int mxN = 3e5 + 5;
int a[mxN];
int n, d;
int ans[mxN];
template<typename T>
struct SegmentTree {
vector<T> t;
int n;
T identity = T();
SegmentTree() { }
SegmentTree(int _n) : n(_n) {
int lg = 31 - __builtin_clz(n);
if ((1 << lg) < n) lg++;
lg++;
t.assign((1 << lg) + 5, identity);
}
void update(int x, int lx, int rx, int p, T v) {
if (lx == rx) {
t[x] = v;
return;
}
int mx = (lx + rx) >> 1;
if (p <= mx) update(x << 1, lx, mx, p, v);
else update(x << 1 | 1, mx + 1, rx, p, v);
t[x] = max(t[x << 1], t[x << 1 | 1]);
}
void update(int p, T v) {
update(1, 1, n, p, v);
}
T query(int x, int lx, int rx, int l, int r) {
if (lx > r || l > rx) return identity;
if (l <= lx && r >= rx) return t[x];
int mx = (lx + rx) >> 1;
return max(query(x << 1, lx, mx, l, r), query(x << 1 | 1, mx + 1, rx, l, r));
}
T query(int l, int r) {
return query(1, 1, n, l, r);
}
};
SegmentTree<int> st;
multiset<int> s[mxN];
void testCase() {
cin >> n >> d;
map<int, int> mp;
for (int i = 1; i <= n; i++) cin >> a[i], mp[a[i]];
int id = 1;
for (auto &[x, y] : mp) y = id++;
for (int i = 1; i <= n; i++) a[i] = mp[a[i]];
st = SegmentTree<int> (n);
for (int i = 1; i <= n; i++) s[i].insert(0);
vector<int> ans(n + 1);
for (int i = 1; i <= n; i++) {
ans[i] = st.query(1, a[i] - 1) + 1;
s[a[i]].insert(ans[i]);
st.update(a[i], *s[a[i]].rbegin());
if (i - d >= 1) {
s[a[i - d]].erase(s[a[i - d]].find(ans[i - d]));
st.update(a[i - d], *s[a[i - d]].rbegin());
}
}
cout << *max_element(ans.begin(), ans.end()) << "\n";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
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... |