hdu 1087

求的是最长上升子序列的和。
数组dp[i]表示以i结尾的最长上升子序列的和,因为开始每个数字都可以单独的成为一个子序列,所以初始化dp[i]用num[i]。


参考文章:算法笔记p433

#include<algorithm>
#include<cstdio>
using namespace std;

const int len=1001;
int dp[len],num[len];
int main(){
    int n;
    while(~scanf("%d",&n)&&n){
        int ans=0;
        for(int i=0;i<n;i++){
            scanf("%d",&num[i]);
            dp[i]=num[i];
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<i;j++){
                if(num[i]>num[j]&&dp[j]+num[i]>dp[i])   dp[i]=dp[j]+num[i];
            }
            ans=max(ans,dp[i]);
        }
        printf("%d\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.51cto.com/13688928/2109064
今日推荐