HDU1003 Max Sum(求最大字段和)

事实上这连续发表的三篇是一模一样的思路,我就厚颜无耻的再发一篇吧!

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003

----------------------------------------------------------------------------------------------------------------------------------------------------------
欢迎光临天资小屋http://user.qzone.qq.com/593830943/main

----------------------------------------------------------------------------------------------------------------------------------------------------------

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

代码例如以下:

#include <cstdio>
#define INF 0x3fffffff
#define M 100000+17
int a[M];
int main()
{
	int n, i, T, k = 0;
	while(~scanf("%d",&T))
	{
		while(T--)
		{
			scanf("%d",&n);
			int s = 1, e = 1, t = 1;
			int sum = 0, MAX = -INF;
			for(i = 1; i <= n; i++)
			{
				scanf("%d",&a[i]);
				sum+=a[i];
				if(sum > MAX)
				{
					s = t;
					e = i;
					MAX = sum;
				}
				if(sum < 0)
				{
					t = i+1;
					sum = 0;
				}
			}
			printf("Case %d:\n",++k);
			printf("%d %d %d\n",MAX,s,e);
			if(T!=0)
				printf("\n");
		}
	}
	return 0;
}
时间: 2024-10-18 19:12:59

HDU1003 Max Sum(求最大字段和)的相关文章

HDU1003 Max Sum

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 题意:给你一组数字,求出最大的字段和. 思路:这是一个经典的dp题目,定义数组a储存一组数字,a[j]为ji个数,dp[j]表示已j结尾的最大字段和,那么dp[j]=max(dp[j-1]+a[j],dp[j]). 例如: a[]       6   -1   5    4    -7 dp[]     6    5   10  14    7 代码如下: #include <iostream

解题报告:hdu1003 Max Sum - 最大连续区间和 - 计算开头和结尾

2017-09-06 21:32:22 writer:pprp 可以作为一个模板 /* @theme: hdu1003 Max Sum @writer:pprp @end:21:26 @declare:连续区间最大和 @data:2017/9/6 */ #include <bits/stdc++.h> using namespace std; int main() { //freopen("in.txt","r",stdin); int cas; cin

HDU1003 Max Sum 最大子序列和的问题【四种算法分析+实现】

就拿杭电OJ上的第1003题开始吧,这题比原书要复杂一些. 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 fi

hdu1003 Max Sum(经典dp )

A - 最大子段和 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u 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 thi

ACM学习历程—HDU1003 Max Sum(dp &amp;&amp; 最大子序列和)

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 inte

HDU--1003 Max Sum(最大连续子序列和)

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

HDU1003 Max Sum(最大连续子序列和)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1003 简单dp,状态转移方程:sum[i] = max{sum[i-1]+a[i],a[i]}. (sum[i]记录以a[i]为子序列末端的最大连续和.) 对于a[i]这个数字,我们考虑是否将它选入之前连续的序列. 如果选,状态变为sum[i-1]+a[i] ; 如果不选,则从此开始一个新的序列,故和为a[i]. 1 #include<cstdio> 2 int main() 3 { 4 int T,

HDU 1003 Max Sum 求区间最大值 (尺取法)

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 294096    Accepted Submission(s): 69830 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max s

HDU1003 Max Sum 解题报告

目录 题目信息 Problem Description Input Output Sample Input Sample Output 题解 思路 源代码 题目信息 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