hdu 1003 MaxSum 最大子段和

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

#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 100010;
const int inf = 0x7fffffff;

int n,l,r;
int a[maxn];
long long maxSum(){
    long long cur = 0,res=-inf;
    int temp = 1;
    for (int i = 1; i <= n; i++){
        cur += 1ll*a[i];
        if (cur > res){
            res = cur;
            l = temp;
            r = i;
        }
        if (cur < 0){
            cur = 0;
            temp = i + 1;
        }
    }
    return res;
}
int main()
{
    int t,k=1;
    cin >> t;
    while (t--){
        cin >> n;
        for (int i = 1; i <= n; i++)
            cin >> a[i];
        cout << "Case " << k++ << ":" << endl;
        cout << maxSum()<<" ";
        cout<< l <<" "<< r << endl;
        if (t != 0) cout << endl;
    }
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/looeyWei/p/10502347.html
今日推荐