杭电1003-Max Sum
Max Sum
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).
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.
#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int n,m,a,temp,start,end;
long sum,max;
cin>>n;
for(int j=1;j<=n;j++)
{
cin>>m;
sum=0;
max=-1001;////由于存在全是负数,将最大值赋值为负数(更大也行)
temp=1;
for(int i=1;i<=m;i++)
{
cin>>a;
sum+=a;
if(sum>max)
{
max=sum;
start=temp;//更新起始点
end=i; //更新末点
}
if(sum<0)
{
sum=0;
temp=i+1;//执行下一个i(也就是读取下一个数字)
}
}
cout<<"Case "<<j<<":"<<endl;
cout<<max<<" "<<start<<" "<<end<<endl;
if(j<n)
cout<<endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
杭电1003-Max Sum的更多相关文章
- 杭电1003 Max Sum 【连续子序列求最大和】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目意思: 即给出一串数据,求连续的子序列的最大和 解题思路: 因为我们很容易想到用一个max ...
- 杭电1003 Max Sum TLE
这一题目是要求连续子序列的最大和,所以在看到题目的一瞬间就想到的是把所有情况列举出来,再两个两个的比较,取最大的(即为更新最大值的意思),这样的思路很简单,但是会超时,时间复杂度为O(n^3),因为有 ...
- 杭电 1003 Max Sum (动态规划)
参考:https://www.cnblogs.com/yexiaozi/p/5749338.html #include <iostream> #include <cstdio> ...
- 杭电1003 MAX SUN
Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum ...
- HDU 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
- HDOJ(HDU).1003 Max Sum (DP)
HDOJ(HDU).1003 Max Sum (DP) 点我挑战题目 算法学习-–动态规划初探 题意分析 给出一段数字序列,求出最大连续子段和.典型的动态规划问题. 用数组a表示存储的数字序列,sum ...
- hdu 1003 Max Sum (DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1003 Max Sum Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行
测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...
- HDU 1003 Max Sum【动态规划求最大子序列和详解 】
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- #1003 Max Sum
http://acm.hdu.edu.cn/showproblem.php?pid=1003 给你一串数列,让你求出其中 一段连续的子数列 并且 该子数列所有数字之和最大,输出该子数列的和.起点与终点 ...
随机推荐
- 分布式环境下限流方案的实现redis RateLimiter Guava,Token Bucket, Leaky Bucket
业务背景介绍 对于web应用的限流,光看标题,似乎过于抽象,难以理解,那我们还是以具体的某一个应用场景来引入这个话题吧. 在日常生活中,我们肯定收到过不少不少这样的短信,“双11约吗?,千款….”,“ ...
- 帮你深入理解OAuth2.0协议
1. 引言 如果你开车去酒店赴宴,你经常会苦于找不到停车位而耽误很多时间.是否有好办法可以避免这个问题呢?有的,听说有一些豪车的车主就不担心这个问题.豪车一般配备两种钥匙:主钥匙和泊车钥匙.当你到酒店 ...
- Oracle 监听器日志文件过大导致监听异常
Oracle 监听器日志文件过大导致监听异常 db版本:11.2.0.1 os版本:windows2008 现象: 应用异常,无法连接数据库.登陆数据库服务器,查看监听已经断掉.尝试重启监听,重启失败 ...
- uiwebview加载中文URL
uiwebview加载中文URL _paperGamePath = [_paperGamePath stringByAddingPercentEscapesUsingEncoding:NSUTF8St ...
- networkcommon v3开源消息框架资料
http://www.cnblogs.com/csdev/category/870400.html
- 别人写的一个Bootstrap系列教程
http://www.cnblogs.com/lansy/category/659061.html
- IOS推荐学习网站
1> 个人博客:技术大牛 唐巧:http://blog.devtang.com/blog/archives/ 王巍:http://www.onevcat.com 破船之家:http://beyo ...
- pg_stat_statements
Functions pg_stat_statements_reset() returns void pg_stat_statements_reset discards all statistics g ...
- DO.NET操作数据库
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- $.trim()函数
$.trim(str) 返回:string: 参数str :String类型,需要去除两端空白字符的字符串.如果参数str不是字符串类型,该函数将自动将其转为字符串(一般调用其toString()方法 ...