HDU 1087 E

http://acm.hdu.edu.cn/showproblem.php?pid=1087

设dp[i]表示去到这个位置时的最大和值。(就是以第i个为结尾的时候的最大值)

那么只要扫描一遍dp数组,就能得到ans,因为最后一步可以无条件到达终点。

那么可以用O(n^2)转移,枚举每一个位置,其中要初始化dp值,dp[i] = a[i],意思就是到达第i个位置的时候,和值最小也是

a[i]把,因为无论如何也可以一步到达a[i]这个值。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL;

#include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
const int maxn = 1000 + 20;
LL dp[maxn];
int n;
int a[maxn];
void work() {
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    for (int i = 1; i <= n; ++i) {
        dp[i] = a[i];
        for (int j = 1; j < i; ++j) {
            if (a[j] >= a[i]) continue;
            dp[i] = max(dp[i], dp[j] + a[i]);
        }
    }
    LL ans = -1e18L;
    for (int i = 1; i <= n; ++i) {
        ans = max(dp[i], ans);
    }
    cout << ans << endl;
}

int main() {
#ifdef local
    freopen("data.txt","r",stdin);
#endif
    IOS;
    while (cin >> n && n) work();
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/liuweimingcprogram/p/6001653.html