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;
typedef long long ll;
typedef long double ld;
#define pb push_back
int main() {
int n, d;
cin >> n >> d;
vector<int> v(n);
for (int i = 0; i < n; i++) cin >> v[i];
do {
// compression
vector<int> dd = v;
sort(dd.begin(), dd.end());
dd.resize(unique(dd.begin(), dd.end()) - dd.begin());
for (int i = 0; i < n; i++) {
v[i] = lower_bound(dd.begin(), dd.end(), v[i]) - dd.begin();
}
} while (false);
int mx = *max_element(v.begin(), v.end());
if (d == 1) {
stack<int> s;
int ans = 0;
for (int i = n; i--;) {
while (s.size() && s.top() <= v[i]) s.pop();
s.push(v[i]);
ans = max(ans, (int)s.size());
}
cout << ans << endl;
return 0;
}
#ifdef HZLOCAL
cout << mx << endl;
cout << "V: ";
for (auto x : v) cout << x << " ";
cout << endl;
#endif
vector<vector<int>> dp(n, vector<int>(mx + 1, 0));
vector<multiset<int>> cmax(mx + 1);
for (int i = 0; i < n; i++) {
int prev, cur = 0;
for (int j = 0; j <= mx; j++) {
prev = cur;
if (cmax[j].size()) {
cur = max(cur, *cmax[j].rbegin());
}
if (j < v[i]) continue;
int candidate = max(cur, j ? dp[i][j - 1] : 0);
if (v[i] == j) candidate = max(candidate, j ? prev + 1 : 1);
dp[i][j] = candidate;
}
if (i >= d) {
for (int j = 0; j <= mx; j++) {
cmax[j].erase(cmax[j].find(dp[i - d][j]));
}
}
for (int j = 0; j <= mx; j++) {
cmax[j].insert(dp[i][j]);
}
}
#ifdef HZLOCAL
for (auto x : dp) {
for (auto y : x) cout << y << " ";
cout << endl;
}
#endif
cout << *max_element(dp[n - 1].begin(), dp[n - 1].end()) << endl;
}
# | 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... |