最长不下降/不上升子序列&&最长上升/下降子序列

最长不下降/不上升子序列&&最长上升/下降子序列

struct cmp1{bool operator()(int a,int b){return a>b;}};
int main()
{
    while(cin>>a[++n]);
    n--;
    if(n==0)
    {
        cout<<0<<endl<<0<<endl;
        return 0;
    }
    dp1[1]=a[1];dp2[1]=a[1];
    dp3[1]=a[1];dp4[1]=a[1];
    int len1=1,len2=1,len3=1,len4=1;
    for(int i=2;i<=n;i++)
    {
        if(a[i]<=dp1[len1])dp1[++len1]=a[i];//最长不上升子序列
        else
        {
            int j=upper_bound(dp1+1,dp1+len1+1,a[i],cmp1())-dp1;
            dp1[j]=a[i];
        }
        if(a[i]>dp2[len2])dp2[++len2]=a[i];//最长上升子序列
        else
        {
            int j=lower_bound(dp2+1,dp2+len2+1,a[i])-dp2;
            dp2[j]=a[i];
        }
        if(a[i]>=dp3[len3])dp3[++len3]=a[i];//最长不下降子序列
        else
        {
            int j=upper_bound(dp3+1,dp3+len3+1,a[i])-dp3;
            dp3[j]=a[i];
        }
        if(a[i]<dp4[len4])dp4[++len4]=a[i];//最长下降子序列
        else
        {
            int j=lower_bound(dp4+1,dp4+len4+1,a[i],cmp1())-dp4;
            dp4[j]=a[i];
        }
    }
    cout<<len1<<endl<<len2<<endl<<len3<<endl<<len4<<endl;
    return 0;
}

 

posted @ 2019-06-20 15:12  杯酒朝阳  阅读(732)  评论(0编辑  收藏  举报