This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
struct Fenwick {
vector<multiset<int>> F;
int n;
Fenwick(int n_) : n(n_) {
F.resize(n);
}
void remove(int x, int d) {
for (; x < n; x |= x + 1) F[x].erase(F[x].find(d));
}
void add(int x, int d) {
for (; x < n; x |= x + 1) F[x].insert(d);
}
int qry(int x) {
int s = 0;
for (; x >= 0; x = (x & (x + 1)) - 1)
if (F[x].size()) s = max(s, *F[x].rbegin());
return s;
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, d;
cin >> n >> d;
vector<int> a(n);
for (int i = 0; i < n; i++) cin >> a[i];
vector<int> v = a;
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (int &x : a) x = lower_bound(v.begin(), v.end(), x) - v.begin();
Fenwick fen(n);
vector<int> dp(n);
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
multiset<int> st;
for (int i = 0; i < n; i++) {
int mn = (i == 0 ? 0 : *st.begin());
while (pq.size()) {
auto [x, y] = pq.top();
if (mn > x) {
fen.remove(x, y);
pq.pop();
} else break;
}
dp[i] = fen.qry(a[i] - 1) + 1;
fen.add(a[i], dp[i]);
st.insert(a[i]);
if (i >= d) {
pq.push({a[i - d], dp[i - d]});
st.erase(st.find(a[i - d]));
}
}
cout << *max_element(dp.begin(), dp.end()) << '\n';
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... |