1259: [Example 9.3] is not decreasing sequence of the longest seek

Portal: http: //ybt.ssoier.cn: 8088 / problem_show.php pid = 1259?

Description [title]

Is provided by the n (1≤n≤200) non-identical composition of the integer number of columns, referred to as: b (1), b ( 2), ......, b (n) and b (i) ≠ b (j ) (i ≠ j), if present i1 <i2 <i3 <... < ie and there b (i1) <b (i2 ) <... <b (ie) is called the length of the sequence e does not decrease. Program requires, when the original number is listed, does not decrease the longest sequence determined.

For example 13,7,9,16,38,24,37,18,44,19,21,22,63,15.

Examples 13,16,18,19,21,22,63 is a length 7 sequence is not reduced, but also 7, 8 of the length 9,16,18,19,21,22,63 composition does not decrease sequence.

[Enter]

First row n, the behavior of the second space-separated by n integers.

[Output]

The maximum number of first output behavior max (see sample form);

The second drop is not formed acts max integer sequence, the answer may not be unique, the output can be one kind, this problem special evaluation.

[Sample input]

14
13 7 9 16 38 24 37 18 44 19 21 22 63 15

[Sample Output]

max=8
7 9 16 18 19 21 22 63

 




I really spent a lot of time, say on the subject of each number are not equal, there is actually equal.

#include<iostream>
#include<queue>
#include<cstring>
using namespace std; 
#define N 200+1
long long int a[N],dp[N][3],n,ans[3];
void find(int k)
{
    if(dp[k][2]!=-1)
    find(dp[k][2]);
    cout<<a[k]<<' ';
}
void print()
{
    cout<<"max="<<ans[1]<<endl;
    find (years [ 2 ]);
    cout<<endl;
}
int main ()
{
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<=n;i++)dp[i][1]=1;
    for(int i=1;i<=n;i++)dp[i][2]=-1;
    for(int i=2;i<=n;i++)
        for(int j=1;j<i;j++)
            if(a[j]<=a[i])
                if(dp[i][1]<dp[j][1]+1)
                {
                    dp[i][1]=max(dp[i][1],dp[j][1]+1);
                    dp[i][2]=j;
                }
    for(int i=1;i<=n;i++)
    {
        if(ans[1]<dp[i][1])
        {
            ans[1]=dp[i][1];
            years [ 2 ] = i;
        }
    }
    print();
}

 


Guess you like

Origin www.cnblogs.com/jzxnl/p/11141660.html