sdnu 1172.Queue 【LIS】

1172.Queue

Time Limit: 1000 MS    Memory Limit: 32768 KB
Total Submission(s): 91    Accepted Submission(s): 31

Description

    On the PE,the teacher wants to choose some of n students to play games. Teacher asks n students stand in a line randomly(obviously,they have different height), then teacher tells someone to leave the queue(relative position is not change)to make the left students keep a special queue: 
    Assuming that the left students’ numbers are 1, 2,……, m from left to right and their height are T1,T2,……,Tm. Then they satisfy T1<T2<......<Ti, Ti><t2<……<ti,ti>Ti+1>……>Tm (1<=i<=m).
    Giving you the height of n students (from left to right), please calculate how many students left at most if you want to keep such a special queue. 

Input

    The first line contains an integer n (2<=n<=1000) represents the number of students.
    The second line contains n integers Ti(150<=Ti<=200) separated by spaces, represent the height(cm) of student i. 

Output

    One integer represents the number of the left students.

Sample Input

8
186 180 150 183 199 130 190 180

Sample Output

5

Hint

 

Source

Unknown
枚举每一个位置的max(正向到此位置的LIS+逆向到此位置的LIS),ans需要-1
#include <bits/stdc++.h>
using namespace std;

int dp1[1005],dp2[1005],a[1005],b[1005];

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            b[n-i+1]=a[i];
        }
        memset(dp1,0,sizeof(dp1));
        memset(dp2,0,sizeof(dp2));
        dp1[1]=1;
        dp2[1]=1;
        for(int i=2;i<=n;i++)
        {
            dp1[i]=1;
            dp2[i]=1;
            for(int j=1;j<i;j++)
            {
                if(a[j]<a[i])
                    dp1[i]=max(dp1[i],dp1[j]+1);
                if(b[j]<b[i])
                    dp2[i]=max(dp2[i],dp2[j]+1);
            }
        }
        int ans=-1;
        for(int i=1;i<=n;i++)
        {
            ans=max(ans,dp1[i]+dp2[n-i+1]);
        }
        cout<<ans-1<<'\n';
    }
    return 0;
}

 

posted @ 2019-07-24 16:24  观稳769  阅读(169)  评论(0编辑  收藏  举报