HDU1003 结题报告(最大子序列和)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weifuliu/article/details/82313100

HDU1003 Max Sum
题面

题解

#include<iostream>
#include<algorithm>
#include<string>
#include<math.h>
#include<set>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const int MAXN = 1e5 + 5;
int a[MAXN];


int main() {
	std::ios::sync_with_stdio(false);
	int t = 1 , n, T;
	int maxSum, l, r, position, thisSum;
	cin >> T;
	while (T--) {
		cin >> n;
		for (int i = 0; i < n; i++) {
			cin >> a[i];
		}
		position = l = r = 0;
		thisSum = maxSum = a[0];
		for (int i = 1; i < n; i++) {
			if (thisSum < 0) {
				thisSum = a[i];
				position = i;
			}
			else {
				thisSum += a[i];
			}
			if (thisSum > maxSum) {
				maxSum = thisSum;
				l = position;
				r = i;
			}
		}

		printf("Case %d:\n", t++);
		cout << maxSum << " " << l+1 << " " << r+1 << endl;
		if (T) {
			cout << endl;
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weifuliu/article/details/82313100