HDU 1003(最大子序列和)

来源:互联网 发布:无线mesh网络特点的是 编辑:程序博客网 时间:2024/06/06 12:43

题意:给出数据组数,每组第一个数为序列的长度n,后面跟着n个数。求这个序列的最大子序列和。

#include <cstdio>#include <climits>void res(){int n, sum = 0, _max = INT_MIN, t, head = 0, s, e;scanf("%d", &n);for (int i = 0; i < n; ++i){scanf("%d", &t);sum += t;if (_max < sum){_max = sum;s = head;e = i;}if (sum < 0){sum = 0;head = i+1;}}printf("%d %d %d\n", _max, s+1, e+1);}int main(){int t;scanf("%d", &t);for (int _t = 1; _t <= t; ++_t){if (_t > 1) puts("");printf("Case %d:\n", _t);res();}return 0;}

0 0
原创粉丝点击