CodeForces 320D Psychos in a Line (單調棧+DP)

#include<bits/stdc++.h>
using namespace std;
#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long
#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define fi first                                  
#define se second
#define mk(x,y) make_pair(x,y)
const int mod=1e9+7;
const int maxn=1e5+10;
const int maxm=5e5+10;
const ll INF=1e18;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){
    if(y==0) return x;
    return gcd(y,x%y);
}
int n,a[maxn],dp[maxn],sk[maxn],cnt=0;
int main(){
    ios::sync_with_stdio(false);/*
    freopen("d://in.txt","r",stdin);
    freopen("d://out.txt","w",stdout);*/
    cin>>n;
    int ans=0;
    rep(i,1,n+1) cin>>a[i];
    a[n+1]=maxn;
    rep(i,1,n+2){
        int tmp=0;
        while(cnt-1>=0&&a[i]>a[sk[cnt-1]]){
            cnt--;
            tmp=max(tmp,dp[sk[cnt]]);
        }
        if(cnt) dp[sk[cnt-1]]=max(dp[sk[cnt-1]],tmp)+1;
        sk[cnt++]=i,dp[i]=0;
        ans=max(ans,tmp);
    }
    cout<<ans<<"\n";
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章