Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 52982 Accepted Submission(s): 11890
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
#include <stdio.h>
#define MAX 100005
int nNum[MAX];
int start, end;
//算法思路:用sum和max来记录临时最大和最终最大,而start end ts te 分别用来记录起点 终点 临时起始点 终点
int cal(int len)
{
int max, i, sum, ts, te;
sum = max = -9999; //让max和sum开始置于无限小
for(i = 0; i < len; i++)
{
if(sum < 0)
{
if(nNum[i] > sum)
{
sum = nNum[i];
ts = te = i;
if(max < sum)
{
max= sum;
start = ts;
end = te;
}
}
}else
{
sum += nNum[i];
te = i;
if(sum > max)
{
max = sum;
start = ts;
end = te;
}
}
}
return max;
}
int main()
{
int t, i, n, j;
// freopen("input.in", "r", stdin);
scanf("%d", &t);
for(i = 1; i <= t; i++)
{
scanf("%d", &n);
for(j = 0; j < n; j++)
scanf("%d", &nNum[j]);
int max = cal(n);
printf("Case %d:\n", i);
printf("%d %d %d\n", max, start + 1, end + 1);
if(i != t)
printf("\n");
}
return 0;
}
分享到:
相关推荐
### Hdu1003:最大子序列和 本题属于动态规划的经典应用,要求找出一个数组中连续子序列的最大和。代码中定义了两个辅助数组`a`和`sum`,其中`sum[i]`表示以`a[i]`结尾的最大子序列和。通过比较`sum[i-1]`和0的大小...
例如,1003 DP 经典问题,最大连续子段和;1024 经典 DP,最大 M 子段和;1025 经典 DP,最长递增子序列(要用 NLogN 的方法过);1051 经典贪心,也可以用 DP;1058 经典问题,丑数,DP;1081 经典 DP 等等。 搜索...
在HDU OJ中,DP题目如1003、1024等,通常需要选手对状态进行定义,并找出状态转移方程,从而求得问题的最优解。这类题目考验的是选手对复杂问题的抽象能力和优化策略的运用。 ### 搜索 搜索算法是解决问题的一种...
简单题(1003):本题目是关于简单的数学运算的应用,需要使用基本的数学概念来解决问题。 知识点:数学基础知识、简单运算 4. 渊子赛马 排序+贪心的方法归并(1004):本题目是关于排序算法和贪心算法的应用,...
和 http://acm.hdu.edu.cn/showproblem.php?pid=1003) 这个问题寻找数组中的最大连续子序列和。状态方程可以表示为:`sum[i]=max(sum[i-1]+a[i], a[i])`,或者采用双指针方法进行遍历计算。 4. **Largest ...
1001 计算直线的交点数 1002 FatMouse's Speed1003 Common Subsequence1004 Max Sum 1005 Super Jumping! Jumping! Jumping! 1006 免费馅饼 1007 Humble Numbers1008 Monkey and Banana 1009 龟兔赛跑 1010 数塔
1003 1004 1006 1007 专题分类 (一)简单搜索 ID Problem C++ Source 1 HDU 2553 (精简版) 2 HDU 1312 3 POJ 3984 4 POJ 2251 5 POJ 3278 6 POJ 3279 7 ZOJ 1002 8 POJ 1321 9 HDU 1241 (二)树 ID Problem C++ Source...
1003 1004 1006 1007 专题分类 (一)简单搜索 ID Problem C++ Source 1 HDU 2553 (精简版) 2 HDU 1312 3 POJ 3984 4 POJ 2251 5 POJ 3278 6 POJ 3279 7 ZOJ 1002 8 POJ 1321 9 HDU 1241 (二)树 ID Problem C++ Source...
3. **字符串处理**:"1003.cpp"和"1010.cpp"可能涉及到字符串处理,包括模式匹配、最长公共子序列、编辑距离等算法,这对于处理文本信息至关重要。 4. **数值计算与数学**:"10106.cpp"和"2074.cpp"可能涉及到数值...
- **POJ 1003 - Find The Closest Pair From N Lines** #### 5. 最远点对 ##### 5.1 基本原理 - **问题描述**:给定平面上的一组点,求出其中两点之间的最大距离。 - **解决方法**:通常可以通过构建凸包来简化...