Max Sum

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

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

分析:

第一次是用暴力解法,时间复杂度为O(n*n),果然TLE了!!

上网查了之后才明白这是个动态分配的题。大体思路是依次遍历data数组,若sum>=0,则令sum+=data[i],否则sum=data[i],然后比较sum和max,若sum>max,则令max=sum,并修改相应的子列起初下标start和截至下标end。这样一次遍历下来之后就找到了和最大的子列。时间复杂度为O(n)。

注意输出格式,输出每个Case的结果后空一行(除最后一个Case)。

下面给出AC代码。

#include<cstdio>
using namespace std; int main(){
int T,cas=0;
int data[100005];
scanf("%d",&T); //输入T(测试用例个数)
while(T--){
int max=-1e8,sum=0,start=0,end=0,s=0; //初始时给max一个很小的值
printf("Case %d:\n",++cas);
int n;
scanf("%d",&n); //输入序列大小
for(int i=0;i<n;i++){
scanf("%d",&data[i]);
if(sum>=0){
sum+=data[i];
}
else{
sum=data[i];
s=i; //s为当前序列的起始下标
}
if(sum>max){
max=sum;
start=s;
end=i;
}
}
printf("%d %d %d\n",max,start+1,end+1);
if(T!=0)
printf("\n");
}
return 0;
}

hdoj1003 DP的更多相关文章

  1. hdoj1003【DP】

    这道题目的DP,写到现在才明白... 每次加或者不加的条件就是这个前面这个子序列合是不是大于等于0,如果不是加了就会让这个位置的值没有意义,如果大于0,他还是在往递增的方向继续前进. 以及这个条件的继 ...

  2. 【集训笔记】动态规划【HDOJ1159【HDOJ1003

    终于开始DP了] HDOJ_1159  Common  Subsequence 题目链接 Sample Input abcfbc abfcab programming contest abcd mnp ...

  3. BZOJ 1911: [Apio2010]特别行动队 [斜率优化DP]

    1911: [Apio2010]特别行动队 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 4142  Solved: 1964[Submit][Statu ...

  4. 2013 Asia Changsha Regional Contest---Josephina and RPG(DP)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4800 Problem Description A role-playing game (RPG and ...

  5. AEAI DP V3.7.0 发布,开源综合应用开发平台

    1  升级说明 AEAI DP 3.7版本是AEAI DP一个里程碑版本,基于JDK1.7开发,在本版本中新增支持Rest服务开发机制(默认支持WebService服务开发机制),且支持WS服务.RS ...

  6. AEAI DP V3.6.0 升级说明,开源综合应用开发平台

    AEAI DP综合应用开发平台是一款扩展开发工具,专门用于开发MIS类的Java Web应用,本次发版的AEAI DP_v3.6.0版本为AEAI DP _v3.5.0版本的升级版本,该产品现已开源并 ...

  7. BZOJ 1597: [Usaco2008 Mar]土地购买 [斜率优化DP]

    1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4026  Solved: 1473[Submit] ...

  8. [斜率优化DP]【学习笔记】【更新中】

    参考资料: 1.元旦集训的课件已经很好了 http://files.cnblogs.com/files/candy99/dp.pdf 2.http://www.cnblogs.com/MashiroS ...

  9. BZOJ 1010: [HNOI2008]玩具装箱toy [DP 斜率优化]

    1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 9812  Solved: 3978[Submit][St ...

随机推荐

  1. $.meta ? $.extend({}, opts, $this.data()) : opts 是什么

    问:$.meta ? $.extend({}, opts, $this.data()) : opts 是什么 答:这应该是一个jQuery扩展插件中的代码,其中运用了三目运算符,以及jQuery的ex ...

  2. Windows下MySQL免安装版的安装、卸载

    一.安装 1.下载 到MySQL官网http://dev.mysql.com/downloads/mysql/ 下载mysql-5.6.15-win32.zip. 2.拷贝 将mysql-5.6.15 ...

  3. (转)RRU交织冗余在LTE-R组网中的应用研究

    RRU交织冗余在LTE-R组网中的应用研究 王 芳1,2 庞萌萌1,2 (1.北京全路通信信号研究设计院集团有限公司,北京 100070; 2.北京市高速铁路运行控制系统工程技术研究中心,北京 100 ...

  4. PHP学习方法总结

    怎样快速学好PHP技术  PHP学习方法总结   怎样快速学好PHP技术?我想这应该是大多数参加PHP培训学习PHP的同学比较关心和想要知道的问题,今天扣丁学堂小编就给大家简单谈谈怎样快速学好PHP技 ...

  5. Spring Security编程模型

    1.采用spring进行权限控制 url权限控制 method权限控制 实现:aop或者拦截器(本质就是之前之后进行控制)--------------------proxy就是 2.权限模型: 本质理 ...

  6. PHP书写格式

    从一个例子开始. 启动编辑器,创建一个php文件并键入如下代码: <?php echo "你好!"; ?> 运行 将该文件命名为 test.php 并存储于 E:htm ...

  7. ElasticSearch Document API

    删除索引库 可以看到id为1的索引库不见了 这里要修改下配置文件 slave1,slave2也做同样的操作,在这里就不多赘述了. 这个时候记得要重启elasticseach才能生效,怎么重启这里就不多 ...

  8. c++官方文档-枚举-联合体-结构体-typedef-using

    #include<iostream> #include <new> #include<stdio.h> using namespace std; /** * url ...

  9. linux实时流量监控

    在类Unix系统中可以使用top查看系统资源.进程.内存占用等信息.查看网络状态可以使用netstat.nmap等工具.若要查看实时的网络流量,监控TCP/IP连接等,则可以使用iftop. 一.if ...

  10. CUDA C Programming Guide 在线教程学习笔记 Part 7

    ▶ 可缓存只读操作(Read-Only Data Cache Load Function),定义在 sm_32_intrinsics.hpp 中.从地址 adress 读取类型为 T 的函数返回,T ...