HDU   1003(A - 最大子段和)

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87125#problem/A

题目:

Max Sum

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
 
 
题意:
给出一个序列,求此序列的最大子段和 及开始和结束的位置。
 
分析:
动态规划。求最大子段和。b[i]表示最大子段和。c[i]表示子段和开始的位置。b[i]=(b[i-1]+a[i])>a[i]?b[i-1]+a[i]:a[i]
 
代码:
 #include<cstdio>
#include<iostream>
using namespace std;
const int maxn=; int a[maxn],b[maxn],c[maxn];//b表示最大子段和,c表示开始的位置 int main()
{
int t,m=;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
b[]=a[];//记开始时的子段和为b[1]
c[]=;//开始时的位置为1
for(int i=;i<=n;i++)
{
if(b[i-]>=)
{
b[i]=b[i-]+a[i];//子段和
c[i]=c[i-];
}
else
{
b[i]=a[i];
c[i]=i;
}
}
int max=b[];//令起始位置最大值为b[1]
int end=;
for(int i=;i<=n;i++)
{
if(b[i]>max)
{
max=b[i];
end=i;//结束位置
}
}
printf("Case %d:\n",m++);
printf("%d %d %d\n",max,c[end],end);
if(t)
printf("\n");
}
return ;
}

我先在杭电上做了几遍,提交一直都是WA,改了几次都是WA。第一次是因为我直接把开始位置写为1,根本没有计算c[i]。后来又出现了PE,因为我没有写if(t)。改了几次终于改成功了。

 
 

HDU 1003(A - 最大子段和)的更多相关文章

  1. HDU 1003 最大连续子段和

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others)M ...

  2. HDOJ(HDU).1003 Max Sum (DP)

    HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...

  3. dp 动态规划 hdu 1003 1087

    动态规划就是寻找最优解的过程 最重要的是找到关系式 hdu 1003 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目大意:求最大字序列和, ...

  4. hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行

    测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...

  5. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  6. 【ToReadList】六种姿势拿下连续子序列最大和问题,附伪代码(以HDU 1003 1231为例)(转载)

    问题描述:       连续子序列最大和,其实就是求一个序列中连续的子序列中元素和最大的那个. 比如例如给定序列: { -2, 11, -4, 13, -5, -2 } 其最大连续子序列为{ 11, ...

  7. hdu 1003 hdu 1231 最大连续子序列【dp】

    HDU1003 HDU1231 题意自明.可能是真的进步了点,记得刚开始研究这个问题时还想了好长时间,hdu 1231还手推了很长时间,今天重新写干净利落就AC了. #include<iostr ...

  8. [ACM] hdu 1003 Max Sum(最大子段和模型)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

  9. HDU 1003:Max Sum(DP,连续子段和)

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Su ...

随机推荐

  1. [LeetCode]题解(python):139-Word Break

    题目来源: https://leetcode.com/problems/word-break/ 题意分析: 给定一个字符串s和一个字典dict,判断s是不是由字典dict里面的元素组成的. 题目思路: ...

  2. Android使用ViewFlipper实现左右滑动效果面

    在我的博客中,上次是使用ViewPager实现左右滑动的效果的,请看文章:Android使用ViewPager实现左右滑动效果. 这次我来使用ViewFlipper实现这种效果,好了,先看看效果吧: ...

  3. python 内部函数,以及lambda,filter,map等内置函数

    #!/usr/bin/python #encoding=utf-8 def back(): return 1,2, "xxx" #python 可变参数 def test(*par ...

  4. 如何写出专业级OOP程序-----文档注释

    由于时间的限制就写一些通用的注释啦> @author 姓名 这个标记将产生一个作者条目,可以使用多个@author注释,每个对应一个作者. @version 文本 这个标记产生版本条目,对当前版 ...

  5. SQL数据库关键字和列名冲突处理

    在设计SQL数据库的时候可能由于考虑不全,使列名和数据库内关键字冲突,可能导致Query不能被正确识别,对列名要加[]处理.

  6. ##DAY4 事件的基本概念、触摸的基本概念、响应者链、手势

    ##DAY4  事件的基本概念.触摸的基本概念.响应者链.手势 #pragma mark ———————事件的基本概念 ——————————— 事件的基本概念: 1)事件是当用户的手指触击屏幕及在屏幕 ...

  7. Android应用开发基础篇(11)-----ViewFlipper

    链接地址:http://www.cnblogs.com/lknlfy/archive/2012/03/01/2376067.html 一.概述 ViewFlipper这个部件是用来实现多页显示的,多页 ...

  8. servlet操作数据库

    工具:myeclipse 数据库工具:mysql java ee操作数据库,首先要导入数据库驱动文件,我用的是mysql 刚开始,很多人代码正确但是就是连接不上,原因就是忘了驱动文件的导入. 我的驱动 ...

  9. [LeetCode]题解(python):134-Gas Station

    题目来源: https://leetcode.com/problems/gas-station/ 题意分析: 在一个圈子路线里面有N个汽油站,i站的汽油有gas[i]汽油.现在有一辆无限容量的车,它从 ...

  10. 高质量程序设计指南C/C++语言——C++/C常量(2)