# 最大子序列和 HDOJ 1003 Max Sum

第二种办法：一个前缀记录前i个数字的和，那么ans = sum - mn; mn表示前j个和且和最小

两种办法都是O (n) 1003就这么难？？ 推荐学习资料：六种姿势拿下连续子序列最大和问题　　最大子序列和问题

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 1e6 + 10;
const int INF = 0x3f3f3f3f;
int a[N];

//O (n)
void MCS(int n)	{
int l = 0, ll = 0, rr = 0;
int sum = -INF, mx = -INF;
for (int i=1; i<=n; ++i)	{
if (sum + a[i] < a[i])	{
sum = a[i];	l = i;
}
else	sum += a[i];
if (sum > mx)	{
mx = sum;	ll = l, rr = i;
}
}
printf ("%d %d %d\n", mx, ll, rr);
}

int main(void)	{
int T, cas = 0;	scanf ("%d", &T);
while (T--)	{
int n;	scanf ("%d", &n);
for (int i=1; i<=n; ++i)	scanf ("%d", &a[i]);
printf ("Case %d:\n", ++cas);
MCS (n);
if (T)	puts ("");
}

return 0;
}


//O (n) //another
void MCS(int n)	{
int l = 0, ll = 0, rr = 0;
int sum = 0, mx = -INF, mn = 0;
for (int i=1; i<=n; ++i)	{
sum += a[i];
if (sum - mn > mx)	{
mx = sum - mn;	ll = l;	rr = i;
}
if (sum < mn)	{
mn = sum;	l = i;
}
}
printf ("%d %d %d\n", mx, ll + 1, rr);
}

posted @ 2015-08-10 19:13  Running_Time  阅读(186)  评论(0编辑  收藏  举报