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 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>
int main()
{
int T,k, R, L, Max, sum, t, total=, n;
scanf("%d", &T);
while(T--)
{
sum = Max =-; //足够小就好
scanf("%d", &n);
for(int i=; i<=n; i++)
{
scanf("%d", &t);
if (sum<)
{
sum = t, k = i;
}
else
sum += t;
if (Max < sum) Max = sum, L = k, R = i; //L R分别为初始和结束位置
}
printf("Case %d:\n", total++);
printf("%d %d %d\n", Max, L, R );
if (T) printf("\n");
}
return ;
}
#include<iostream>
using namespace std;
int a[],b[];
int main()
{
int n,T,s,t,max,total,k=;
cin>>T;
while(T--)
{
cin>>n;
for(int i=; i<=n; i++)
cin>>a[i];
b[]=a[];
for(int i=; i<=n; i++)
{
if(b[i-]<)
b[i]=a[i];
else
b[i]=b[i-]+a[i];
}
max=b[];
s=;
for(int i=; i<=n; i++)
{
if(b[i]>max)
{
max=b[i];
s=i;
}
}
t=s;
total=;
for(int i=s; i>=; i--)
{
total+=a[i];
if(total==max) t=i;
}
cout<<"Case "<<k++<<":"<<endl;
cout<<max<<" "<<t<<" "<<s<<endl;
if(T) cout<<endl;
}
}

hdu1003 Max Sum(经典dp )的更多相关文章

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

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

  2. 杭电60题--part 1 HDU1003 Max Sum(DP 动态规划)

    最近想学DP,锻炼思维,记录一下自己踩到的坑,来写一波详细的结题报告,持续更新. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Problem ...

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

    Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub ...

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

    2017-09-06 21:32:22 writer:pprp 可以作为一个模板 /* @theme: hdu1003 Max Sum @writer:pprp @end:21:26 @declare ...

  5. Max Sum (dp)

    Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub-sequence. F ...

  6. 最大子序列和——HDU-1003 Max Sum

    题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义dp[i]表示以a[i]为结尾的子序列的和的最大值,因而最大 ...

  7. hdu 1003 Max sum(简单DP)

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

  8. HDU-1003 Max Sum(动态规划,最长字段和问题)

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

  9. HDU1003 Max Sum(求最大字段和)

    事实上这连续发表的三篇是一模一样的思路,我就厚颜无耻的再发一篇吧! 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 -------------- ...

随机推荐

  1. poj3294 Life Forms(后缀数组)

    [题目链接] http://poj.org/problem?id=3294 [题意] 多个字符串求出现超过R次的最长公共子串. [思路] 二分+划分height,判定一个组中是否包含不小于R个不同字符 ...

  2. JavaScript之作用域与作用域链

    今天是2016的第一天,我们得扬帆起航踏上新的征程了.此篇阐述JavaScript中很重要的几个概念:作用域与作用域链及相关知识点. 我们先从变量与作用域的行为关系开始讨论. 变量作用域 JavaSc ...

  3. sql server常用查询

    最近在做一些练习,觉得数据的查询是一个很有意思的,在做的过程中一些好的查询方法也使自己感觉到数据库的强大,于是乎就会想到要把这些方法记下来,以后就懒得再去想了 1.查询是整百的倍数 SELECT 实缴 ...

  4. python通过SMTP发送邮件失败,报错505/535

    python通过SMTP发送邮件失败:错误1:smtplib.SMTPAuthenticationError: (550, b'User has no permission')    我们使用pyth ...

  5. 高性能MySql进化论(九):查询优化器常用的优化方式

    1        介绍 1.1     处理流程 当MYSQL 收到一条查询请求时,会首先通过关键字对SQL语句进行解析,生成一颗“解析树”,然后预处理器会校验“解析树”是否合法(主要校验数据列和表明 ...

  6. 3Dmax导出fbx文件缺失纹理问题

  7. HDU--杭电--1253--胜利大逃亡--广搜

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  8. [Falcor] Indroduce to Model

    How to work with JSON data indirectly through a Falcor Model. The Falcor Model allows you to work wi ...

  9. Java 理论与实践: 用弱引用堵住内存泄漏---转载

    要让垃圾收集(GC)回收程序不再使用的对象,对象的逻辑 生命周期(应用程序使用它的时间)和对该对象拥有的引用的实际 生命周期必须是相同的.在大多数时候,好的软件工程技术保证这是自动实现的,不用我们对对 ...

  10. ubuntu下安装tomcat和配置mysql

    1.到官网 http://jakarta.apache.org/tomcat-7.0.2.tar.gz 安装文件,JDK假设已经安装完毕,接下来直接安装tomcat-7.0.2.tar.gz # ls ...