HDU 1087 Super Jumping! Jumping! Jumping!

本文讨论了一个算法问题,即在一组整数中找到一个递增子序列,使得其元素之和最大。通过使用动态规划的方法,作者解决了这个问题,并提供了一个n^2复杂度的递推公式来实现解决方案。

这个题一看就是找一个 递增子序列。。 然后脑残的以为找 最长上升子序列。 在模板里面加上个 sum 数组递推上 过了样例就交上去了


获得了 WA之后。 自己出了一组数据   3 2 1 8 9   这五个数  最长上升子序列  有三个  3 89  或者 2 8 9 或者 1 8 9 然后就没法处理。


后来一想 应该是 上升子序列中找和最大的。  就写出  n^2 的递推公式了。  s【i】 = max (s【i】,s【j】 + a【i】)  其中 s【i】 代表以第i个数为结尾的最大和。


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <fstream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <vector>
#include <cmath>
#include <iomanip>
typedef long long LL;
typedef unsigned long long LLU;
const double PI=acos(-1.0);
using namespace std;
#define MAXN 1000+10
#define INF 1 << 30
int main (){
    int n;
    while(scanf("%d",&n) && n){
        LL a[MAXN];
        LL s[MAXN] = {0};
        for(int i = 1; i <= n; i++){
            scanf("%I64d",&a[i]);
            s[i] = a[i];
        }
        LL M = 0;
        for(int i = 1; i <= n; i++){
            for(int j = 1; j <= i; j++){
                if(a[i] > a[j])
                    s[i] = max(s[i], s[j]+a[i]);
            }
        }
        for(int i = 1; i <= n; i++)
            M = max(M, s[i]);
        printf("%I64d\n",M);
    }
    return 0;
}

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值