HDU-1003 max sum

求区间连续最大和,DP之。

/*
 * HDU-1003 max sum
 * mike-w
 * 2011-8-30
 * ----------------
 * HINT:DP?
 * ----------------
 * 最大连续子序列和
 * 参见 http://hi.baidu.com/%D1%A9%CE%E8%B1%F9%B7%E2acm/blog/item/0430af0d6c3197e6aa645712.html
 */
#include<stdio.h>

int main(void)
{
	long T,N;
	long sum,start,end;
	long t,tsum,ts;
	long i,j;

#ifndef ONLINE_JUDGE
	freopen("1003.in","r",stdin);
#endif

	scanf("%ld",&T);
	for(i=1;i<=T;i++)
	{
		scanf("%ld",&N);
		start=ts=1;
		sum=-(1<<30); /* attention here! */
		tsum=0;
		for(j=1;j<=N;j++)
		{
			scanf("%ld",&t);
			tsum+=t;
			if(tsum<t)
				tsum=t,ts=j;
			if(sum<tsum)
				sum=tsum,start=ts,end=j;
		}
		printf("Case %ld:\n%ld %ld %ld\n",i,sum,start,end);
		if(i<T) putchar('\n');
	}
	return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值