HDU 1858 Max Partial Value I

求连续子序列的最大和

为毛简单的入门DP没有思路啊。。

学习下别人的解法,理解起来倒还是很容易的。

 1 //#define LOCAL
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 const int maxn = 1000000 + 10;
 9 const int INF = -10000000;
10 
11 struct Node
12 {
13     long long w;
14     int l, r;
15 }node[maxn];
16 
17 long long a[maxn];
18 
19 int main(void)
20 {
21     #ifdef LOCAL
22         freopen("1858in.txt", "r", stdin);
23     #endif
24 
25     int N;
26     scanf("%d", &N);
27     while(N--)
28     {
29         int n;
30         scanf("%d", &n);
31         int i;
32         for(i = 1; i <= n; ++i)
33         {
34             scanf("%I64d", &a[i]);
35             node[i].w = a[i];
36             node[i].l = node[i].r = i;
37         }
38 
39         int ans = 1;
40         for(i = 2; i <= n; ++i)
41         {
42             if(node[i-1].w+a[i] >= node[i].w)
43             {
44                 node[i].w = node[i-1].w + a[i];
45                 node[i].l = node[i-1].l;
46             }
47             if(node[i].w > node[ans].w)
48                 ans = i;
49         }
50 
51         printf("%I64d %d %d\n", node[ans].w, node[ans].l, node[ans].r);
52     }
53     return 0;
54 }
代码君

转载于:https://www.cnblogs.com/AOQNRMGYXLMV/p/3865347.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值