Max Sum (dp)
InputThe 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).
OutputFor 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
题意:找出最大的连续字串和;
#include<iostream>
#include<map>
using namespace std;
int main()
{
int t,mark=,cnt=;
cin >> t;
while (t--)
{
if (cnt) cout << endl;
cnt = ;
mark++;
int temp = , frist, end;
int n,ko,sum=,max=-;
cin >> n;
for (int i = ; i < n; i++)
{
cin >> ko;
sum += ko;
if (sum > max)
{
max = sum; frist = temp; end = i + ;
}
if (sum < )
{
sum = ; temp = i + ;
}
}
cout << "Case " << mark << ":" <<endl<< max << " " << frist << " " << end << endl;
}
return ;
}
注意:因为要输出下标,必须灵活应用temp这个中间值,刚开始因为一直没仔细考虑下标的情况,导致负数情况下不能出正确答案;
通过temp的加入后,可以在大于max的条件满足下更改最后的下标,小于零的情况下可以更改temp,到累计大于max时,直接改frist;
(经典DP题)
Max Sum (dp)的更多相关文章
- HDU 1003 Max Sum(DP)
点我看题目 题意 : 就是让你从一个数列中找连续的数字要求他们的和最大. 思路 : 往前加然后再判断一下就行. #include <iostream> #include<stdio. ...
- HDU 1003:Max Sum(DP,连续子段和)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Su ...
- 杭电60题--part 1 HDU1003 Max Sum(DP 动态规划)
最近想学DP,锻炼思维,记录一下自己踩到的坑,来写一波详细的结题报告,持续更新. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Problem ...
- ACM学习历程—HDU1003 Max Sum(dp && 最大子序列和)
Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub ...
- UVA 10891 Game of Sum(DP)
This is a two player game. Initially there are n integer numbers in an array and players A and B get ...
- HDU-1003:Max Sum(优化)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- hdu 1003 Max Sum (动态规划)
转载于acm之家http://www.acmerblog.com/hdu-1003-Max-Sum-1258.html Max Sum Time Limit: 2000/1000 MS (Java/O ...
- URAL 1146 Maximum Sum(DP)
Given a 2-dimensional array of positive and negative integers, find the sub-rectangle with the large ...
- 杭电 1003 Max Sum (动态规划)
参考:https://www.cnblogs.com/yexiaozi/p/5749338.html #include <iostream> #include <cstdio> ...
随机推荐
- mysql 修改文件记录:
增: insert t1(id, name) values(1, "alex"), (2, "wusir"), (3, "dabing" ...
- JNDI学习总结——Tomcat下使用C3P0配置JNDI数据源
一.C3P0下载 C3P0下载地址:http://sourceforge.net/projects/c3p0/files/?source=navbar
- vue请求java服务端并返回数据
最近在自学vue怎么与java进行数据交互.其实axios还是挺简单的,与ajax请求几乎一样,无外乎也就是要解决下跨域的问题. 废话不多说了,直接贴代码,一看就懂! //向springmvc Con ...
- jq的stop
jQuery stop() 方法用于停止动画或效果,在它们完成之前. stop() 方法适用于所有 jQuery 效果函数,包括滑动.淡入淡出和自定义动画. $(selector).stop(stop ...
- mysql 半同步复制~ 整体概述与改进
一 简介:今天来聊聊增强半同步复制这一强悍的特性 二 原理解析 1 AFTER_COMMIT(5.6默认值) master将每个事务写入binlog ,传递到slave 刷新到磁盘(relay log ...
- EL表达式 EL函数 自定义el函数 《黑马程序员_超全面的JavaWeb视频教程vedio》
\JavaWeb视频教程_day12_自定义标签JSTL标签库,java web之设计模式\day12_avi\12.EL入门.avi; EL表达式 1. EL是JSP内置的表达式语言! * jsp2 ...
- pwnable.kr col
collision - 3 pt 连接上查看col.c源码 分析一下代码, 1.hashcode等于一个固定的值 2.check_password函数取输入数据,4个一组,将输入的字符转成int,然后 ...
- QR 编码原理(三)
一.日本汉字(KANJI)是两个字节表示的字符码,编码的方式是将其转换为13字节的二进制码制. 转换步骤为: 1.对于JIS值为8140(hex) 到9FFC(hex)之间字符: a)将待转换的JIS ...
- [sklearn] 实现随即梯度下降(SGD)&分类器评价参数查看
直接贴代码吧: 1 # -*- coding:UTF-8 -*- 2 from sklearn import datasets 3 from sklearn.cross_validation impo ...
- 20165325 2017-2018-2 《Java程序设计》结对编程_第一周:四则运算
一.码云链接 项目名称FAO 码云链接 二.需求分析 实现一个命令行程序: 自动生成小学四则运算题目(加.减.乘.除) 支持整数 支持多运算符(比如生成包含100个运算符的题目) 支持真分数 统计正确 ...