이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
int n,d;
int a[300005];
int dp[300005];
int posmic[300005];
int posmare[300005];
int pozs[300005];
deque<int> dqmic;
deque<int> dqmare;
signed main()
{
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>d;
for(int i=1;i<=n;i++)
{
cin>>a[i];
while(!dqmic.empty() && a[dqmic.back()]>=a[i])
dqmic.pop_back();
if(!dqmic.empty())
posmic[i] = dqmic.back();
else
posmic[i] = 0;
dqmic.push_back(i);
}
for(int i=1;i<=n;i++)
{
int ult = i;
pozs[i] = i;
for(int j=i-1;j>0;j--)
{
if(ult-j>d)
break;
if(a[j]<a[i])
{
pozs[i] = j;
ult = j;
}
}
}
int rez=0;
for(int i=1;i<=n;i++)
{
dp[i]=0;
int x = posmic[i];
while(x>=pozs[i] && a[x]<a[i])
{
dp[i] = max(dp[i], dp[x]);
x = posmare[x];
}
//for(int j=posmic[i];j>=pozs[i];j--)
// if(a[j]<a[i])
// dp[i]=max(dp[i],dp[j]);
dp[i]++;
rez=max(rez,dp[i]);
while(!dqmare.empty() && dp[dqmare.back()]<=dp[i])
dqmare.pop_back();
if(!dqmare.empty())
posmare[i] = dqmare.back();
else
posmare[i] = 0;
dqmare.push_back(i);
}
cout<<rez;
return 0;
}
/**
dp[i] = scorul maxim care se poate obtine daca ultimul element care creste scorul se afla pe pozitia i
dp[i] = max(dp[x]) + 1, a[x] < a[i], distanta dintre oricare 2 pozitii consecutive cu numere < a[i] este maxim d
x = primul element din stanga lui i < a[i]
repeta atat timp a[x] < a[i]:
x = primul element din stanga lui x > a[x]
folosim binary lifting
*/
# | 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... |