이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define F first
#define S second
using namespace std;
const int N = 3e5+5;
int n, d;
int a[N], dp[N], nx[N];
void input() {
cin >> n >> d;
for (int i = 0; i < n; i++)
cin >> a[i];
}
void solve1() {
int ans = 0;
for (int i = n-1; i > -1; i--) {
int t = 0;
for (int j = i+1; j < n; j++) {
if (a[j] <= a[i]) {
t = 0;
continue;
}
t++;
dp[i] = max(dp[i], dp[j]);
if (t == d)
break;
}
dp[i]++;
ans = max(ans, dp[i]);
}
cout << ans << endl;
}
void solve2() {
stack<int> st;
for (int i = 0; i < n; i++) {
while (st.size() && a[st.top()] < a[i]) {
nx[st.top()] = i;
st.pop();
}
st.push(i);
}
while (st.size()) {
nx[st.top()] = n;
st.pop();
}
dp[n] = 0;
for (int i = n-1; i > -1; i--)
dp[i] = dp[nx[i]] + 1;
int ans = 1;
for (int i = 0; i < n; i++)
ans = max(ans, dp[i]);
cout << ans;
}
int main() {
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
input();
if (d == 1)
solve2();
else
solve1();
}
# | 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... |