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 ...
随机推荐
- 开发框架-开发组合:LAMP(Web应用软件组合)
ylbtech-开发框架-开发组合:LAMP(Web应用软件组合) LAMP是指一组通常一起使用来运行动态网站或者服务器的自由软件名称首字母缩写. 1.返回顶部 1. 中文名:lamp 外文名:lam ...
- GROUP BY关键字与WITH ROLLUP一起使用
1 GROUP_CONCAT mysql> SELECT student_name, -> GROUP_CONCAT(test_score) -> FROM stud ...
- 6.22-Servlet
一.servlet servlet是运行在服务器端的java程序 jsp专注于显示 servlet处理请求和响应 创建servlet 继承HttpServlet 实现servlet接口 配置servl ...
- CSS border边框属性教程(color style)
CSS 边框即CSS border-border边框样式颜色.边框样式.边框宽度的语法结构与应用案例教程篇 一.CSS 边框基础知识 CSS 边框即CSS border是控制对象的边框边线宽度.颜色. ...
- ICML论文|阿尔法狗CTO讲座: AI如何用新型强化学习玩转围棋扑克游戏
今年8月,Demis Hassabis等人工智能技术先驱们将来到雷锋网“人工智能与机器人创新大会”.在此,我们为大家分享David Silver的论文<不完美信息游戏中的深度强化学习自我对战&g ...
- js中的数组操作
<!DOCTYPE HTML> <html > <head> <meta http-equiv="Content-Type" conten ...
- tcp协议和udp协议的使用场景
一:什么是TCP(Transmission Control Protocol,传输控制协议) tcp是面向连接的协议,也就是说,在收发数据前,必须和对方建立可靠的连接.一个TCP连接必须要经过三次 ...
- Mysql-两表的连接,copy表,select的各种用法
-- 连接:外连接,内连接 两个表之间 外连接:right join left join -- left join 左标为主 一般以值少的为主 select * from table1 left ...
- 4. java乱码处理
//返回到jsp页面 //request.setCharacterEncoding("utf-8"); //response.setContentType("text/h ...
- bootStrap的小知识
代码模块 <code> 内联代码 <kbd> 用户输入 <pre>代码段 <var>数学字符 <samp>程序输出 表格 <thead ...