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. 移动端的click

    移动端的click 移动端click和touch的关系 搬运 http://segmentfault.com/q/1010000000691822 手指在屏幕上滑动时 各个touch的触发顺序 tou ...

  2. 黑马程序员_<<StringBuffer,包装类>>

    --------------------ASP.Net+Android+IOS开发..Net培训.期待与您交流! -------------------- 1. StringBuffer 1.概述 S ...

  3. 一步一步学数据结构之1--n(通用树)

    今天来看大家介绍树,树是一种非线性的数据结构,树是由n个结点组成的有限集合,如果n=0,称为空树:如果n>0,则:有一个特定的称之为根的结点,它只有直接后继,但没有直接前驱:除根以外的其他结点划 ...

  4. 假设给Contact的List加一个用字母排序的导航

    效果图: 这样写Layout: <? xml version="1.0" encoding="utf-8"? > <LinearLayout ...

  5. wpf 如何设置滚动条在超出范围的时候才显示?(转)

    VerticalScrollBarVisibility="Auto"  垂直自动显示 HorizontalScrollBarVisibility="Auto" ...

  6. DevOps探索

    devops最近随着docker的升温而被越来越多的人所吸引!最近因项目所需投身到devops的项目当中,经过初步的实践搞出一套 paas平台的devops,这个平台现在还需要检验! 作为一个dev, ...

  7. Lucence.Net+添加关键词+分页+排序

    1.使用queryparser完成解析搜索请求 2.基本格式如: QueryParser parser=new QueryParser("字段名称","分析器实例&quo ...

  8. JavaScript 运动框架 Step by step

    http://blog.csdn.net/rsj217/article/details/7986905 关于offsetLeft:http://www.cnblogs.com/JackJiang/ar ...

  9. 03-C语言编码规范和变量

    目录: 一.C语言的编码规范 二.变量 三.浮点型float 四.变量名命名规则 五.变量作用域与生命周期 回到顶部 一.C语言的编程规范 1 语句可以分开放在任意位置 2 空格可以让代码更清晰 3 ...

  10. 射频识别技术漫谈(14)——Mifare S50与S70的存取控制

    存取控制指符合什么条件才能对卡片进行操作. S50和S70的块分为数据块和控制块,对数据块的操作有“读”.“写”.“加值”.“减值(含传输和存储)”四种,对控制块的操作只有“读”和“写”两种. S50 ...