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> ...
随机推荐
- ubuntu14.04 Samba服务无法访问 网络名不再可用的问题
参考链接 : https://blog.csdn.net/liuyixjtu/article/details/54575514
- Django REST framework 第三章 CBV
从介绍Django快开始,我们就一直在使用FBV的方式来撰写代码,二者本质上并没有太大的区别,然而到了REST framework,更会倾向于用CBV来写API的视图,后面会看到这个方式的强大,它允许 ...
- Python基础1(2017-07-16)
先墨守常规写一个简单的hello world!这是所有语言学习的第一点. Python Hello world name="hello world!" print(name) 如上 ...
- EL知识点总结
EL知识点总结 - canger - 博客园http://www.cnblogs.com/canger/p/6675799.html 1 只可以获取内置对象的属性值,不可以获取JSP页面中局部java ...
- macbook install mysql
安装Homebrew,详细步骤参见Homebrew官网. brew doctor确认brew在正常工作. brew update更新包. brew install mysql 安装mysql.log如 ...
- python中“*”、"*args"、"kwargs"三种用法
参考链接:https://www.cnblogs.com/cwind/p/8996000.html 注意的是: (1)"*"符号的用法很类似C++中的指针,针对列表; (2)&qu ...
- Keepalived详解(五):Keepalived集群中MASTER和BACKUP角色选举策略【转】
一.Keepalived集群中MASTER和BACKUP角色选举策略 在keepalived集群中,其实并没有严格意义上的主.备节点,虽然可以在keepalived配置文件中设置state选项为MAS ...
- java官网真不错
要用到一个软件,打开时说没找到javaw,然后大家说这是因为没安装java,就按着链接去了java官网 https://www.java.com/zh_CN/download/faq/remove_o ...
- codeforces 38G - Queue splay伸展树
题目 https://codeforces.com/problemset/problem/38/G 题意: 一些人按顺序进入队列,每个人有两个属性,地位$A$和能力$C$ 每个人进入时都在队尾,并最多 ...
- 装机uefi问题
1 uefi不支持普通winpe启动,需要改为legacy 2 七代酷睿不支持win10以下版本 3 装机后无法启动是因为隐藏分区问题,删除掉,最好整个硬盘格式化 4 老毛桃一键装机不可以时,改为虚拟 ...