[杭电]Max Sum

来源:互联网 发布:java中的各种buffer 编辑:程序博客网 时间:2024/06/05 01:10

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1003


时间复杂度: O(n)


代码:

#include <iostream>#include <string>using namespace std;int num[100005];int main(){int T;cin >> T;int N;for (int i = 1; i <= T; i++){cin >> N;for (int j = 0; j < N; j++){cin >> num[j];}int max, maxhere;int start, end;max = maxhere = num[0];start = end = 0;//index表示可能的初始位置,只有在max改变时start才会为indexint index = 0;for (int j = 1; j < N; j++){if (maxhere < 0){index = j;maxhere = num[j];}else{maxhere += num[j];}if (max < maxhere){max = maxhere;start = index;end = j;}}if (i>1){cout << endl;}cout << "Case " << i << ":" << endl;cout << max << " " << start+1 << " " << end+1 << endl;}return 0;}

0 0
原创粉丝点击