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. MVC简单的解释

    MVC (Model-View-Controller,模型视图控制器)是一种软件的设计模式,它最早是由 20 世纪 70 年代的 Smalltalk 语言提出的,即把一个复杂的软件工程分解为三个层 ...

  2. Redis学习笔记(三)-数据类型之string类型

    string是redis最基本的类型,而且string类型是二进制安全的.意思是redis的string可以包含任何数据.比如jpg图片或者序列化的对象.从内部实现来看其实string可以看作byte ...

  3. 创建dml触发器

    -实现删除学生信息时把该学生的成绩记录全部清空 --判断触发器是否存在 if exists(select * from sysobjects where name = 'delete_student' ...

  4. Dynamics CRM查询实体共享给哪些人

    在mscrm中,如果想查询一条记录共享给了哪些人,需要用到PrincipalObjectAccess 表,sql如下: select u.FullName,a.RC_name,sup.SystemUs ...

  5. WinDbg使用

    1.抓dump文件 程序崩溃(crash)的时候, 为了以后能够调试分析问题, 可以使用WinDBG要把当时程序内存空间数据都保存下来,生成的文件称为dump 文件. 步骤: 1) 打开WinDBG并 ...

  6. 关于python学习路线

    *Python进阶(目录/书籍/学习路线) 忘了从哪里转的了,没办法标记哦,实在不好意思... 目录:) 1. 简介&helloworld&开发环境配置 2.基本语法:标识符& ...

  7. Redis 之list链表结构及命令详解

    1.lpush  key   value   从左放一个值 2.rpush  key   value  从右放一个值 3.lrange  key  start   stop  获取链表数据(start ...

  8. Docker系列之入门

    Docker基本介绍 一.什么是Docker 在docker的官方之什么是docker中提到了一句话:“当今各大组织或者团体的创新都源于软件(例如OA.ERP等),其实很多公司都是软件公司" ...

  9. [CodeForces]908D New Year and Arbitrary Arrangement

    设状态f[i][j]表示有i个a,j个ab的期望 发现如果i+j>=k的话就再来一个b就行了. #include <iostream> #include <cstdio> ...

  10. 当样式中存在!important时无法使用show()或hide() 2017-06-11 22:25 15人阅读 评论(0) 收藏

    如果使用!important在你的样式中,比如display: none !important,此时就不能用show()了 但是我在查阅资料时发现有这样的解释, If using !important ...