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;
#define i64 long long
#define mp make_pair
#define all(x) (x).begin(), (x).end()
const int MAXN = 300005;
const int INF = 1e9 + 7;
int N, D, a[MAXN], f[MAXN], r[MAXN], from[MAXN];
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> N >> D;
for (int i = 1; i <= N; i ++) cin >> a[i];
stack<int> s; s.push(N + 1); a[N + 1] = INF, r[N + 1] = N + 1, from[N + 1] = N + 1;
int ans = 0;
for (int i = N; i >= 1; i --) {
while (!s.empty() && a[s.top()] <= a[i]) s.pop();
r[i] = s.top();
int greater = 1;
f[i] = f[s.top()] + 1;
from[i] = s.top();
int en = N + 1;
for (int j = s.top() + 1; j <= en; j ++) {
if (greater >= D) break;
if (a[j] > a[i]) {
if (f[j] + 1 > f[i]) {
from[i] = j;
f[i] = f[j] + 1;
en = min(en, from[j]);
}
}
else greater = 0;
greater += (a[j] > a[i]);
}
ans = max(ans, f[i]);
s.push(i);
// cout << i << " " << from[i] << " " << f[i] << "\n";
}
cout << ans << "\n";
}
# | 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... |