1007 Maximum Subsequence Sum (25 point(s))

题解

水题。

#include<iostream>
#include<cstdio>
using namespace std;
const int INF = 0x3f3f3f3f;
int first, last, sum, temp, cnt;
int n, l, a[10000];
int main() {
	scanf("%d", &n);
	for(int i = 0; i < n; ++i) scanf("%d", &a[i]);
	l = a[0]; temp = -INF;
	for(int i = 0; i < n; ++i) {
		sum += a[i];
		if(sum > temp) {
			first = l;
			last = a[i];
			temp = sum;	
		}
		if(sum < 0) {
			l = a[i + 1];
			sum = 0;
			cnt++;	
		}
	}
	if(cnt == n) printf("0 %d %d", a[0], a[n - 1]); // read document 
	else	printf("%d %d %d", temp, first, last);
	return 0;
}

 

相关文章:

  • 2018-10-05
  • 2021-08-05
  • 2021-10-09
  • 2019-08-24
  • 2021-09-04
  • 2021-12-18
  • 2021-09-11
  • 2021-07-07
猜你喜欢
  • 2020-05-18
  • 2021-01-23
  • 2021-11-28
  • 2021-11-13
  • 2021-08-19
  • 2021-04-26
  • 2018-07-14
相关资源
相似解决方案