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;
int main() {
cin.tie(0)->sync_with_stdio(0);
int n, d;
cin >> n >> d;
vector<int> A(n);
for (int i=0; i<n; i++) cin >> A[i];
if (d == 1) {
stack<int> s;
int ans = 0;
for (int i=n-1; i>=0; i--) {
while (!s.empty() && s.top() <= A[i]) s.pop();
s.push(A[i]);
ans = max(ans, (int)s.size());
}
cout << ans << "\n";
} else {
vector<int> dp(n,1);
int ans = 0;
for (int i=0; i<n; i++) {
vector<int> minpath(n);
multiset<int> path;
for (int j=max(0,i-d); j<i; j++) {
if (A[j] < A[i]) dp[i] = max(dp[i], dp[j]+1);
if (A[j] == A[i]) dp[i] = max(dp[i], dp[j]);
minpath[j] = A[j];
path.insert(A[j]);
}
for (int j=i-d-1; j>=0; j--) {
minpath[j] = max(*path.begin(), A[j]);
if (A[j] >= minpath[j]) {
if (A[j] < A[i]) dp[i] = max(dp[i], dp[j]+1);
if (A[j] == A[i]) dp[i] = max(dp[i], dp[j]);
}
path.erase(path.find(minpath[j+d]));
path.insert(minpath[j]);
}
ans = max(ans, dp[i]);
}
cout << ans << "\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... |