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. jQuery屏蔽浏览器的滚动事件,定义自己的滚轮事件

    1.首先应用jQuery库 ,不做详细介绍 2引用jQuery的mousewheel库,这里面是这个库的源码,使用时直接拷贝过去就可以了: (function(a){function d(b){var ...

  2. MapReduce C++ Library

    MapReduce C++ Library for single-machine, multicore applications Distributed and scalable computing ...

  3. Docker集群管理(二)—— docker+swarm+etcd+shipyard

    引言 前一篇介绍如何简单的搭建一个可视化管理的docker集群,本篇将在此基础之上引入etcd发现服务. 目的 使用etcd发现服务解决swarm内置发现服务的不稳定问题.etcd采用raft算法,这 ...

  4. 香侬科技独家对话Facebook人工智能研究院首席科学家Devi Parikh

    Facebook 人工智能研究院(FAIR)首席科学家 Devi Parikh 是 2017 年 IJCAI 计算机和思想奖获得者(IJCAI 两个最重要的奖项之一,被誉为国际人工智能领域的「菲尔兹奖 ...

  5. 查看linux服务器CPU数量

    首先,要区分两个概念:物理CPU和逻辑CPU. 物理CPU就是服务器上实际安装的CPU.但是一个物理CPU可以有多个核.例如,一个 i5 760 是双核,而一个 i5 2250 是四核.如果开启了In ...

  6. python入门第0篇 Windows下python的安装及pip安装和使用

    知识内容: 1. python2和python3安装 2. pip安装及pip命令使用 注:安装python3就可以了,python2除非项目开发需要否则不用安装,目前学习python就使用pytho ...

  7. 在oracle下如何创建database link全面总结

    物理上存放于网络的多个ORACLE数据库,逻辑上可以看成一个单一的大型数据库,用户可以通过网络对异地数据库中的数据进行存取,而服务器之间的协同处理对于工作站用户及应用程序而言是完全透明的,开发人员无需 ...

  8. python的动态性和_slot_

    python是动态语言 1. 动态语言的定义 动态编程语言 是 高级程序设计语言 的一个类别,在计算机科学领域已被广泛应用.它是一类 在运行时可以改变其结构的语言 :例如新的函数.对象.甚至代码可以被 ...

  9. bootstrap修改勾选样式

    小对勾需要引入awesome插件. css部分: .bella-checkbox{ position: relative;}/** 将初始的checkbox的样式改变 */.bella-checkbo ...

  10. HTML5 位运算符

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...