Max Sum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 181284    Accepted Submission(s): 42384

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

/*找出一个数组中和最大的一串数字,求和并输出起点和终点,简单的dp,一个数字一个数字开始加
找到小于0的话就重新开始找,否则就继续加,开始时想用01背包做,但是发现全部的数字加起来可能是
负数,这样的话01背包就没办法列出所有的情况,并且起点和终点也不好记录*/
#include<stdio.h>
#include<string.h>
int a[100010];
int main()
{
int max,b,e,t,n,sum;
int Case=1;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
int k=1;
int max=0,flog=0;
sum=b=e=0;
for(int j=1;j<=n;j++)
{
if(a[j]>0)
flog=1;//如果数组中没有一个数大于0,那么就找出最大的负数
sum+=a[j];
if(sum<0)
{
sum=0;
k=j+1;//如果sum<0,就让sum=0重新开始加
}
if(sum>max)
{
max=sum;//找到比max大的和就开始替换
b=k;
e=j;
}
}
if(flog==0)
{
max=a[1];
b=1,e=1;
for(int j=2;j<=n;j++)
if(max<a[j])
{
max=a[j];
b=j;e=j;
}
}
printf("Case %d:\n",Case++);
printf("%d %d %d\n",max,b,e);
if(t>=1)
printf("\n");
}
return 0;
}
 

Max Sum--hdoj 1003 dp的更多相关文章

  1. HDU 1024 Max Sum Plus Plus --- dp+滚动数组

    HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...

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

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

  3. hdu 1003 Max Sum(基础dp)

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

  4. Max Sum -- hdu -- 1003

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

  5. hdu 1024 Max Sum Plus Plus DP

    Max Sum Plus Plus Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php ...

  6. Max Sum Plus Plus-HDU1024(dp)

    Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b ...

  7. hdu1003 Max Sum(经典dp )

      A - 最大子段和 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Descr ...

  8. HDU OJ Max sum 题目1003

     #include <iostream> #include<stdio.h> #include<stdlib.h> using namespace std; i ...

  9. HDU1024 Max Sum Plus Plus —— DP + 滚动数组

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS ...

  10. hdu Max Sum Plus Plus(dp+滚动数组)

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 m为段,要深刻理解题意,并没有说是段与段要连接. 题解链接:http://blog.csdn.n ...

随机推荐

  1. PANDAS 数据分析初学者教程

    Pandas 初学者教程       2018-05-19 六尺巷人 对于数据科学家,无论是数据分析还是数据挖掘来说,Pandas是一个非常重要的Python包.它不仅提供了很多方法,使得数据处理非常 ...

  2. VS2012快捷操作功能

    VS2003用了6年,感情深厚,最近换工作刚刚接触VS2010,使用一个月感觉VS2010在人性化方面的功能实在是太强悍了,大大提高了写代码的效率,就如同魔兽世界里的快捷键操作一样,左手抚键右手摸鼠, ...

  3. dubbo之事件通知

    事件通知 在调用之前.调用之后.出现异常时,会触发 oninvoke.onreturn.onthrow 三个事件,可以配置当事件发生时,通知哪个类的哪个方法 1. 服务提供者与消费者共享服务接口 in ...

  4. C++对象的内存模型

    1. 普通对象模型 对象是如何在内存中布局的? 成员 存放位置 访问范围 非静态数据成员 每一个对象体内 为该对象专有 静态数据成员 程序的静态存储区内,只有一份实体 为该类所有对象共享 成员函数(静 ...

  5. Shiro Shiro Web Support and EnvironmentLoaderListener

    Shiro Shiro Web Support 主要参考: http://shiro.apache.org/web.html 还有涛哥的 作为资源控制访问的事情,主要使用在网络后台方面,所以了解了本地 ...

  6. vue 登录验证码

    vue 登录验证码 最近在开发pc端项目,配合elementui使用 createCode() { var code = ""; var codeLength = 4; //验证码 ...

  7. 无需编写代码,API业务流程测试,零代码实现

    引言 除了测试单个接口,我们常常需要对多个有数据或者逻辑关联的接口进行业务流程测试,例如获取验证码-注册-登录.传统测试业务流程需要编写一系列测试代码,现在通过eoLinker全UI界面,无需编写任何 ...

  8. Python之CSV模块

    1. CSV简介 CSV(Comma Separated Values)是逗号分隔符文本格式,常用于Excel和数据库的导入和导出,Python标准库的CSV模块提供了读取和写入CSV格式文件的对象. ...

  9. emmmmmm(官宣?)

    实验室永远不会是学习的唯一地点,不是吗? 总后悔当初退竞赛,现在却还是选择退出,大概是自己真的不适合吧...

  10. 51nod1079 中国剩余定理【数论】

    一个正整数K,给出K Mod 一些质数的结果,求符合条件的最小的K.例如,K % 2 = 1, K % 3 = 2, K % 5 = 3.符合条件的最小的K = 23. Input 第1行:1个数N表 ...