题解 | #最大序列和#

最大序列和

http://www.nowcoder.com/practice/df219d60a7af4171a981ef56bd597f7b

因为题目要求连续,所以只能从相邻的状态转移过来。 改一下最长上升子序列这道题的转移方程就可以了

#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

const int N = 1000000 + 10;

int n;
int a[N];
int f[N];

int main()
{
    while (cin >> n)
    {
        for (int i = 1; i <= n; ++i)
            scanf("%d", &a[i]);
        f[1] = a[1];
        for (int i = 2; i <= n; ++i)
        {
            f[i] = a[i];
            f[i] = max(f[i], f[i-1] + a[i]);
        }
        int res = -0x3f3f3f3f;
        for (int i = 1; i <= n; ++i)
            res = max(res, f[i]);
        printf("%d\n", res);
    }
    return 0;
}
全部评论

相关推荐

缒梦&独舞:这家公司是这样的,去年给我实习offer了,不过也是面着玩儿的,他周六还要去做公益志愿活动
点赞 评论 收藏
分享
龙珠传说:nb,公务员解约不需要支付违约金吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务