码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1087&1160 最长增序列问题

时间:2016-04-03 01:48:02      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:

假期第二天,继续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

 

 

 

 

 

hdu 1087&1160 最长增序列问题

标签:

原文地址:http://www.cnblogs.com/EdsonLin/p/5348734.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!