hdoj1003 DP
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的更多相关文章
- hdoj1003【DP】
这道题目的DP,写到现在才明白... 每次加或者不加的条件就是这个前面这个子序列合是不是大于等于0,如果不是加了就会让这个位置的值没有意义,如果大于0,他还是在往递增的方向继续前进. 以及这个条件的继 ...
- 【集训笔记】动态规划【HDOJ1159【HDOJ1003
终于开始DP了] HDOJ_1159 Common Subsequence 题目链接 Sample Input abcfbc abfcab programming contest abcd mnp ...
- BZOJ 1911: [Apio2010]特别行动队 [斜率优化DP]
1911: [Apio2010]特别行动队 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 4142 Solved: 1964[Submit][Statu ...
- 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 ...
- AEAI DP V3.7.0 发布,开源综合应用开发平台
1 升级说明 AEAI DP 3.7版本是AEAI DP一个里程碑版本,基于JDK1.7开发,在本版本中新增支持Rest服务开发机制(默认支持WebService服务开发机制),且支持WS服务.RS ...
- AEAI DP V3.6.0 升级说明,开源综合应用开发平台
AEAI DP综合应用开发平台是一款扩展开发工具,专门用于开发MIS类的Java Web应用,本次发版的AEAI DP_v3.6.0版本为AEAI DP _v3.5.0版本的升级版本,该产品现已开源并 ...
- BZOJ 1597: [Usaco2008 Mar]土地购买 [斜率优化DP]
1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4026 Solved: 1473[Submit] ...
- [斜率优化DP]【学习笔记】【更新中】
参考资料: 1.元旦集训的课件已经很好了 http://files.cnblogs.com/files/candy99/dp.pdf 2.http://www.cnblogs.com/MashiroS ...
- BZOJ 1010: [HNOI2008]玩具装箱toy [DP 斜率优化]
1010: [HNOI2008]玩具装箱toy Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 9812 Solved: 3978[Submit][St ...
随机推荐
- 小峰servlet/jsp(3)登陆功能实现
一.User模型: User.java: package com.java1234.model; public class User { private int id; private String ...
- Java-Runoob-高级教程-实例-数组:08. Java 实例 – 数组填充
ylbtech-Java-Runoob-高级教程-实例-数组:08. Java 实例 – 数组填充 1.返回顶部 1. Java 实例 - 数组填充 Java 实例 以下实例我们通过 Java Ut ...
- jstack可以定位到线程堆栈
java命令--jstack 工具 JVM调优之jstack找出最耗cpu的线程并定位代码 jstack可以定位到线程堆栈,根据堆栈信息我们
- RedisCluster读写分离改造
RedisCluster模式启动的环境中,通过Redis中的每个连接,都可以访问 cluster nodes 访问到所有的服务器列表以及其所处于的角色(master/slave).对于RedisC ...
- CorelDRAW X4常用快捷键大全
材料/工具 CorelDRAW X4 方法 1 F1:帮助信息 F2:缩小 F3:放大 F4:缩放到将所有对象置于窗口中 F5:手绘(Freehand)工具 F6:矩形(Rectangle)工具 F7 ...
- 提示ORA-01144: File size (13107200 blocks) exceeds maximum of 4194303 blocks 最大4194303 block(转)
并不是100g的表空间,是100g的数据文件.一般情况下,单个数据文件的最大为32g.解决方法:1.创建多个数据文件,都不能超过32g2.创建大表空间.create bigfile tablespac ...
- js 获取上传文件的字节数及内容
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- CentOS Apache配置详解
要想在linux上实现网页服务器(www)需要Apache这个服务器软件,不过Apache仅能提供最基本的静态网站数据而已,想要实现动态网站的话,最好还是要PHP与MySQL的支持,所以下面我们将会以 ...
- div+css样式命名规则,值得收藏
div+css样式命名规则,值得收藏 头:header 内容:content/container 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体布局宽度:w ...
- Tornado 中 PyMongo Motor MongoEngine 的性能测试
最近在使用 Tornado 开发 API,数据库选择了 MongoDB,因为想使用 Geo 搜索的特性.Python 可供选择的 MongoDB Drivers 可以在官网查找. 在这些 Driver ...