HDU 1003

Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 128040    Accepted Submission(s): 29659


Problem Description
Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. For example, given (6,-1,5,4,-7), the max sum in this sequence is 6 + (-1) + 5 + 4 = 14.
 

Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line starts with a number N(1<=N<=100000), then N integers followed(all the integers are between -1000 and 1000).
 

Output
For each test case, you should output two lines. The first line is "Case #:", # means the number of the test case. The second line contains three integers, the Max Sum in the sequence, the start position of the sub-sequence, the end position of the sub-sequence. If there are more than one result, output the first one. Output a blank line between two cases.
 

Sample Input
  
  
2 5 6 -1 5 4 -7 7 0 6 -1 1 -6 7 -5
 

Sample Output
  
  
Case 1: 14 1 4 Case 2: 7 1 6
 

Author
Ignatius.L
 

Recommend
We have carefully selected several similar problems for you:   1176  1087  1069  2084  1058 
 

Statistic |  Submit |  Discuss |  Note


题目说明:

看到题目的时候,把题目理解错了,一直看不懂题目是什么意思。

后来反复的读题目,原来是这个意思。(就是求和最大子序列)

拿题目中的输入输出例子举例吧:

2

5 6 -1 5 4 -7

7 0 6 -1 1 -6 7 -5

其中,第一行的2:表示下面连续输入两行。

第二行的第一个数 5 :表示这一行后面有5个数字要输入。(我就是这里理解错了)。

第三行的第一个数 7 表示后面有7个数字要输入。(实际数字为:0 6 -1 1 -6 7 -5 ,不包括前面的数字7)


该题我开始用动态规划+数组做的,估计是空间超出限制了,提交了7-8次,一直WA。

后来 无幻的博客 将数组去掉了,AC成功。

这里直接放出链接:http://blog.csdn.net/akof1314/article/details/4757021


/*
	Name: HDU 1003
	Copyright: Analyst 
	Author: Analyst 
	Date: 04/03/14 19:20
	Description: dev-cpp 5.5.3
*/
#include <stdio.h>

int j, t;
int max(int a, int b)
{
	return a >= b ? a : (t = j,b);      /*临时保存pos1*/
}
int main()
{
	int T, N, i, maxValue;
	int temp, sum, pos1, pos2;
	
	scanf("%d", &T);
	for (i = 0; i < T; ++i)
	{
		scanf("%d%d", &N, &temp);
		pos1 = pos2 = t = 1;
		maxValue = sum = temp;   
		for (j = 2; j <= N; ++j)
		{
			scanf("%d",&temp);
			sum = max(sum+temp,temp);  /*如果相加之和小于该数*/
			if (sum >= maxValue)       
			{
				maxValue = sum;
				if (temp != 0)         /*避免情况:5 0 0 1 0 0*/
					pos1 = t;
				pos2 = j;
			}
		}
		printf("Case %d:\n%d %d %d\n",i+1,maxValue,pos1,pos2);
		if (i != T-1)
			printf("\n");
	}
	return 0;
}

102198922014-03-04 23:20:51Accepted100315MS256K729 BC++Analyst


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值