이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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... |