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. java附件上传下载大字段版

    public int up2(Map map) { StringBuffer insertSQL = new StringBuffer(); insertSQL.append("insert ...

  2. ubuntu中查找软件的安装位置

    ubuntu中的软件可通过图形界面的软件中心安装,也可以通过命令行apt-get install安装.但是安装后的软件在哪个位置呢?这点跟windows环境下安装软件的路径选择不一样.ubuntu中可 ...

  3. [置顶] SpecDD(混合的敏捷方法模型)主要过程概述

    敏捷已成为当今使用最广泛的开发方法.有趣的是,敏捷方法的流行性并不是因为它取代了其他开发方法,相反它与这些方法进行了更好地融合.现实世界众多敏捷项目的成功,也证明了敏捷将走向杂化的未来. SpecDD ...

  4. 快速学习使用 Windows Azure 上的 SharePoint Server 2013

     为了在当今的企业环境中占据一席之地,您需要能够迅速顺应变化和应对挑战.有时,需要及时调整您的SharePoint 基础结构以保持竞争优势. 基础结构即服务可通过随时使用.即付即用的解决方案应对这 ...

  5. Android开发中如何解决加载大图片时内存溢出的问题

    Android开发中如何解决加载大图片时内存溢出的问题    在Android开发过程中,我们经常会遇到加载的图片过大导致内存溢出的问题,其实类似这样的问题已经屡见不鲜了,下面将一些好的解决方案分享给 ...

  6. linux下安装python3.3.4

    下载安装包 # wget http://www.python.org/ftp/python/3.3.4/Python-3.3.4.tgz 解压 # tar -xzvf Python-3.3.4.tgz ...

  7. ubuntu 常用生产环境部署配置测试调优

    1,ubuntu monogdb 安装配置 2,ubuntu jdk1.7,tomcat7安装 3,ubuntu LAMP部署 4,mongodb 远程热备份及恢复 使用自带的mongodump和mo ...

  8. 跟我一起学extjs5(19--模块记录的拖放删除、拖放复制新增)

    跟我一起学extjs5(19--模块记录的拖放删除.拖放复制新增)         网页其中的拖放(drag-drop)是比較有趣的操作,extjs5中非常好的封装了拖放的动作,也有各种类来支持,可是 ...

  9. BootStrap 智能表单系列 二 BootStrap支持的类型简介

    代码如下(链接地址:https://github.com/xiexingen/Bootstrap-SmartForm/blob/master/demo/form1-basic.html): <! ...

  10. 上一篇下一篇 排序 (非ID字段排序)

    网上看了很多关于"上一篇下篇"的文章,可大都是按ID排序. 实际上,很少有按ID排序的. 分享下我的单独排序字段的写法,主要分为ms sql2000 和 ms 2005及以上版本. ...