首页 > 其他分享 >最大子序列和

最大子序列和

时间:2022-08-22 20:13:20浏览次数:43  
标签:cout int cin start ans 序列 tie 最大

https://www.acwing.com/problem/content/1481/

思路:
注意f数组的含义以及集合的划分。

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int a[N];
int f[N];

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	
	f[0] =-1;
	int ans = -1, l = 1, r = 1, start = 1;
	for (int i = 1; i <= n; i++)
	{
		if (f[i-1] < 0)
		{
			 start = i;
		}
		f[i] = max(a[i], f[i - 1] + a[i]);
		if (f[i]>ans)
		{
			ans = f[i];
			l = a[start], r = a[i];
		}
	}
	if (ans < 0) cout << 0<<" "<< a[1]<<" "<< a[n];
	else cout << ans<<" "<< l << " " << r;
	return 0;
}

标签:cout,int,cin,start,ans,序列,tie,最大
From: https://www.cnblogs.com/xjtfate/p/16614087.html

相关文章