Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 138410    Accepted Submission(s): 32144

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
这题是一个经典dp问题,dp思想在于把问题分解成若干个子问题,在子问题最优的情况下得出最终最优结果,所以我们每一步都是建立在前一步是最优的基础之上的。所以解决dp问题,最基本的要在某种情景下,想到当前状态的最优情况是什么样的,最优后,接下来怎么办,不是最优的该怎么变成最优的。这就是我们的状态转移方程。
对于本问题,首先明确,连续的子段! 和最大 ,一个数组给我们,第一个数肯定当前最大,毋庸置疑,那么遇到下一个数,怎么判断和是当前最大呢?我们遇到正数,那肯定直接加,因为加完肯定比不加大,如果是负数呢?加上去,原来的和肯定变小,但不加怎么办呢?
基于以上问题 可以得出状态方程 dp[i]=d[i-1]+a[i]>a[i]?dp[i-1]+a[i]:a[i]  (dp[i]表示当前i下最大的子段和,a[i]是需要处理的数字)什么意思呢,当前数字加到之前的和上面后,如果大于当前数字,那么就执行加的操作,如果小于当前数字,就把当前和最大值dp[i]设置为a[i],可能有人问为什么要设置为a[i]。。记住,如果a[i]你不加上去,意味着你需要从新开始累加和了,我们要求是连续的子段和!!!
利用dp数组的代码:
 #include<iostream>
#include<cstdio>
#include<cstdlib>
//#define LOCAL
using namespace std; int main()
{
#ifdef LOCAL
freopen("d:datain.txt","r",stdin);
freopen("d:dataout.txt","w",stdout);
#endif
int n;
while(scanf("%d",&n)!=EOF)
{
int i,m;
for(i = ; i< n;i++)
{
scanf("%d",&m);
int dp[],a[];
scanf("%d",&a[]);
dp[] = a[]; //当前最大
for(int j = ; j<m;j++) //生成了dp状态数组了
{
scanf("%d",&a[j]);
if(dp[j-]+a[j]<a[j]) //状态转移方程
dp[j]=a[j];
else
dp[j]=dp[j-]+a[j];
}
int Max,End;
Max = dp[];
End = ;
for(int j = ;j<m;j++) //寻找区间
if(Max<dp[j])
{
End = j;
Max = dp[j];
}
int Begin = End;
int temp = ;
for(int j = End;j>=;j--)
{
temp +=a[j];
if(temp==dp[End])
Begin = j;
}
cout<<"Case "<<i+<<":"<<endl<<Max<<" "<<Begin+<<" "<<End+<<endl;
if(i<n-)
cout<<endl;
}
}
return ;
}

简化后不带dp数组的,因为这题在dp问题中是比较简单的。

 //hdu 1003

 #include<stdio.h>
int main()
{ int n;
while(scanf("%d",&n)!=EOF)
{
for(int i = ;i<n;i++)
{
int a;
int Max = -;
int sum = ,m;
int Begin=,End=,flag=;
scanf("%d",&m);
scanf("%d",&a);
Max = sum = a;
for(int j = ;j<m ;j++)
{
scanf("%d",&a);
if(sum<)
{
sum=a;
flag=j;
}
else
{ sum=sum+a;
}
if(Max<sum)
{
Max = sum ;
Begin =flag;
End = j;
}
}
printf("Case %d:\n%d %d %d\n",i+,Max,Begin+,End+);
if(i<n-)
printf("\n");
} }
return ;
}

hdu1003的更多相关文章

  1. hdu1000,hdu1001,hdu1002,hdu1003

    hdu1000 仅仅是为了纪念 #include <cstdio> int main() { int a,b; while (scanf("%d%d",&a,& ...

  2. hdu1003 1024 Max Sum&Max Sum Plus Plus【基础dp】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4302208.html   ---by 墨染之樱花 dp是竞赛中常见的问题,也是我的弱项orz, ...

  3. hdu1003 Max Sum(最大子串)

    https://vjudge.net/problem/HDU-1003 注意考虑如果全为负的情况,特判. 还有输出格式,最后一个输出不用再空行. #include<iostream> #i ...

  4. hdu1003 Max Sum【最大连续子序列之和】

    题目链接:https://vjudge.net/problem/HDU-1003 题目大意:给出一段序列,求出最大连续子序列之和,以及给出这段子序列的起点和终点. 解题思路:最长连续子序列之和问题其实 ...

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

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

  6. HDU1003 简单DP

    Max Sum Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the ...

  7. HDu1003(maxn sum)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABBcAAAMDCAYAAAD5XP0yAAAgAElEQVR4nOy97a8c133n2X+H3xjIC4

  8. hdu1003 dp

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1003 #include<cstdio> #include<algorit ...

  9. hdu1003 dp(最大子段和)

    题意:给出一列数,求其中的最大子段和以及该子段的开头和结尾位置. 因为刚学过DP没几天,所以还会这题,我开了一个 dp[100002][2],其中 dp[i][0] 记录以 i 为结尾的最大子段的和, ...

  10. HDU1003前导和

    简单维护前导和 #include<stdio.h> int main() { ],cas,key=; scanf("%d",&cas); while(cas-- ...

随机推荐

  1. selenium webdriver使用过程中出现Element is not currently visible and so may not be interacted with的处理方法

    参考文章: http://blog.csdn.net/passionboyxie/article/details/28661107 http://www.spasvo.com/ceshi/open/k ...

  2. JAVA JNI

    jni非常好的一篇文章 http://m.blog.csdn.net/article/details?id=22827307 JAVA JNI介绍 http://blog.csdn.net/cyg08 ...

  3. 【转】android电池(四):电池 电量计(MAX17040)驱动分析篇

    关键词:android 电池  电量计  MAX17040 任务初始化宏 power_supply 平台信息:内核:linux2.6/linux3.0系统:android/android4.0 平台: ...

  4. debian msyql 5.1 卸载与安装

    卸载:apt-get autoremove --purge mysql-server-5.1 卸载服务端 apt-get remove mysql-common #一定要卸载(包含配置文件) dpkg ...

  5. ubuntu 14.04 chromium 设备adobe flash player(亲测可行)

    首先,根据浏览器提示下载Adobe Flash Player 插入 install_flash_player_11_linux.x86_64.tar.gz;然后使用sudo tar -xzvf ins ...

  6. SVN中tag branch trunk用法详解

    SVN中tag branch trunk用法详解 2010-05-24 18:32 佚名 字号:T | T 本文向大家简单介绍一下SVN中tag branch trunk用法,SVN中tag bran ...

  7. 【水题递归】【HDU2044】我大沙茶了

    有一只经过训练的蜜蜂只能爬向右侧相邻的蜂房,不能反向爬行.请编程计算蜜蜂从蜂房a爬到蜂房b的可能路线数. 其中,蜂房的结构如下所示.   Input 输入数据的第一行是一个整数N,表示测试实例的个数, ...

  8. 三、Mp3帧分析(数据帧)

    一. 帧 帧头长4字节,是的,是4个字节,共32位. 帧头后面可能有两个字节的CRC 校验,这两个字节的是否存在决定于FRAMEHEADER 信息的第16bit, 为0 则帧头后面无校验,为1 则有校 ...

  9. eclipse安装svn插件,在输入url后,一直卡在in progress界面不懂。

    今天遇到上面的情况.网上找了半天都没有找到解决的办法.后来,仔细比对了一下我的eclipse版本和svn版本.发现svn版本真的太老了.用上新的svn后,立马就可以用了 svn - http://su ...

  10. JS Encoding and Decoding

    //charator Str to Hex function strToHex(str) { var rs = ""; for (var i = 0; i < str.len ...