이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define fastio ios_base :: sync_with_stdio(0), cin.tie(0);
using namespace std;
using ll = long long;
const int mxN = 1e6 + 5, mod = 1e9 + 7;
int n, d, a[mxN], K, f[mxN], r[mxN], dp[mxN];
multiset <int> mt[mxN];
vector <int> del[mxN];
int t[mxN * 4];
void Build(int v = 1, int tl = 1, int tr = n) {
if (tl == tr)
t[v] = a[tl];
else {
int tm = (tl + tr) / 2;
Build(v * 2, tl, tm); Build(v * 2 + 1, tm + 1, tr);
t[v] = min(t[v * 2], t[v * 2 + 1]);
}
}
int get(int l, int r, int v = 1, int tl = 1, int tr = n) {
if (l > tr || r < tl)
return n + 1;
if (l <= tl && tr <= r)
return t[v];
int tm = (tl + tr) / 2;
return min(get(l, r, v * 2, tl, tm), get(l, r, v * 2 + 1, tm + 1, tr));
}
void Upd(int p, int val) {
while (p >= 0) {
f[p] = min(f[p], val);
p = (p & (p + 1)) - 1;
}
}
int Get(int p) {
int ans = n + 1;
while (p <= n) {
ans = min(ans, f[p]);
p |= p + 1;
}
return ans;
}
int b[mxN * 4];
void upd(int pos, int val, int v = 1, int tl = 1, int tr = n) {
if (tl == tr) {
b[v] = val;
return;
}
int tm = (tl + tr) / 2;
if (tm <= pos) upd(pos, val, v * 2, tl, tm);
else upd(pos, val, v * 2 + 1, tm + 1, tr);
b[v] = max(b[v * 2], b[v * 2 + 1]);
}
int req(int l, int r, int v = 1, int tl = 1, int tr = n) {
if (l > tr || r < tl)
return 0;
if (l <= tl && tr <= r) return b[v];
int tm = (tl + tr) / 2;
return max(req(l, r, v * 2, tl, tm), req(l, r, v * 2 + 1, tm + 1, tr));
}
int main() {
fastio;
cin >> n >> d;
for (int i = 1; i <= n; ++i)
cin >> a[i], f[i] = n + 1;
{
vector <int> v;
for (int i = 1; i <= n; ++i) v.push_back(a[i]);
sort(v.begin(), v.end());
for (int i = 1; i <= n; ++i) a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin();
}
Build();
for (int i = n; i > 0; i--) {
r[i] = Get(a[i] + 1);
if (i + d <= n)
Upd(get(i, i + d), i);
}
int ans = 0;
for (int i = 1; i <= n; ++i) {
for (auto j : del[i]) {
mt[a[j]].erase(mt[a[j]].find(dp[j]));
if (mt[a[j]].empty())
upd(a[j], 0);
else
upd(a[j], *(--mt[a[j]].end()));
}
dp[i] = req(0, a[i] - 1) + 1;
if (mt[a[i]].empty())
upd(a[i], dp[i]);
else
upd(a[i], max(dp[i], *(--mt[a[i]].end())));
mt[a[i]].insert(dp[i]);
del[r[i]].push_back(i);
if (r[i] > n) ans = max(ans, dp[i]);
}
cout << ans;
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... |