hdu 1087&1160 最长增序列问题

假期第二天,继续dp计划


这两题都可以看作求最长增序列的长度,只不过题二换做了求这个序列的值

这里有个通式

最优子结构

F(r) = max(F(r),F(i)+1)  仅当(0<r<i<n&&a[r]<a[i])

 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;
const int MAXN = 1e3;
typedef __int64 ll;

int a[MAXN+10];
ll dp[MAXN+10];//记录改点出发的最大子数组

int main()
{
    int n;
    while(scanf("%d",&n),n){
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
            dp[i] = a[i];
        }
        for(int i=n-2;i>=0;i--){
            for(int j=i+1;j<n;j++){
                if(a[i]<a[j]){
                    dp[i] = max(dp[i],a[i]+dp[j]);
                }
            }
        }
        ll maxn = -MAXN;
        for(int i=0;i<n;i++)
            maxn = max(maxn,dp[i]);
        cout<<maxn<<endl;
    }
    //cout << "Hello world!" << endl;
    return 0;
}
View Code

 

 

 

 

 

posted @ 2016-04-03 00:43  iEdson  阅读(102)  评论(0编辑  收藏  举报