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

 

代码如下:

#include <iostream>
using namespace std;
int main()
{
int t;
cin>>t;
if(t>=0&&t<=20)
{
for(int i=1;i<=t;i++)
{
int first,second,temp=1;
int sum=0,max=-1001;
int n,x;
cin>>n;
for(int j=1;j<=n;j++)
{
cin>>x;
sum=sum+x;
if(max<sum)
{
max=sum;
first=temp;
second=j;
}
if(sum<0)
{
sum=0;
temp=j+1;
}
}
cout<<"Case "<<i<<":"<<endl;
cout<<max<<" "<<first<<" "<<second<<endl;
if(i<t)
cout<<endl;
}
}
return 0;
}

杭电1003 MAX SUN的更多相关文章

  1. 杭电1003 Max Sum 【连续子序列求最大和】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1003 题目意思: 即给出一串数据,求连续的子序列的最大和 解题思路: 因为我们很容易想到用一个max ...

  2. 杭电 1003 Max Sum (动态规划)

    参考:https://www.cnblogs.com/yexiaozi/p/5749338.html #include <iostream> #include <cstdio> ...

  3. 杭电1003 Max Sum TLE

    这一题目是要求连续子序列的最大和,所以在看到题目的一瞬间就想到的是把所有情况列举出来,再两个两个的比较,取最大的(即为更新最大值的意思),这样的思路很简单,但是会超时,时间复杂度为O(n^3),因为有 ...

  4. 杭电1003 最大子串(第二次AC) 当作DP的训练吧

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  5. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  6. 2018 Multi-University Training Contest 1 杭电多校第一场

    抱着可能杭电的多校1比牛客的多校1更恐怖的想法 看到三道签到题 幸福的都快哭出来了好吗 1001  Maximum Multiple(hdoj 6298) 链接:http://acm.hdu.edu. ...

  7. 2017杭电ACM集训队单人排位赛 - 6

    2017杭电ACM集训队单人排位赛 - 6 排名 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 59 1 X X 1 1 X X 0 1 ...

  8. 杭电acm 1002 大数模板(一)

    从杭电第一题开始A,发现做到1002就不会了,经过几天时间终于A出来了,顺便整理了一下关于大数的东西 其实这是刘汝佳老师在<算法竞赛 经典入门 第二版> 中所讲的模板,代码原封不动写上的, ...

  9. 高手看了,感觉惨不忍睹——关于“【ACM】杭电ACM题一直WA求高手看看代码”

    按 被中科大软件学院二年级研究生 HCOONa 骂为“误人子弟”之后(见:<中科大的那位,敢更不要脸点么?> ),继续“误人子弟”. 问题: 题目:(感谢 王爱学志 网友对题目给出的翻译) ...

随机推荐

  1. 【转】github更新自己Fork的代码

    github更新自己Fork的代码 时间 2014-03-02 23:25:55  micmiu - 软件开发 原文  http://www.micmiu.com/software/build/git ...

  2. C#实现二叉查找树

    简介 树是一种非线性结构.树的本质是将一些节点由边连接起来,形成层级的结构.而二叉树是一种特殊的树,使得树每个子节点必须小于等于2.而二叉查找树又是一类特殊的二叉树.使得每一个节点的左节点或左子树的所 ...

  3. Android设计模式源码解析之外观模式(Facade)

    https://github.com/simple-android-framework/android_design_patterns_analysis/tree/master/facade/elsd ...

  4. Java遇见HTML——JSP篇之JSP基础语法

    一.JSP简介 JSP全名为Java Server Pages,Java服务器端页面,其根本是一个简化的Servlet设计,它实现了在Java中使用HTML标签.Jsp是一种动态网页技术标准,是在服务 ...

  5. Win7 OpenCV 3.0.0 VS2013 环境配置

    参考资料:http://jingyan.baidu.com/article/75ab0bcbee4b47d6864db2fc.html 注: x86 x64 这些根据自己的系统以及需求而定, 这里就不 ...

  6. Mac 加密软件

    1,花了127块买一个mac 加密软件 hider2,发现他只是把整个文件加密后,加入到某个包里面,然后删除了原文件,这个简单的功能竟然要收取我这么多钱 2. 然后找个开源的加密软件:PGP, 有GU ...

  7. Linux脚本实现“按任意键继续/Press any key to continue”效果

    此代码来自lnmp一键安装包,用于实现“按任意键继续/Press any key to continue”效果: get_char() { SAVEDSTTY=`stty -g` #隐藏终端输入显示 ...

  8. docker summary

    http://blog.tankywoo.com/docker/2014/05/08/docker-4-summary.html 总结的很好 ----------------------------- ...

  9. ali面试点滴

    1.喜欢问原理 最好问2句 回答8句 把相关的知识也加上 最好说一下优势与区别 2.sql优化 分库 分表 ssl https 3.jvm.高并发(事务.原子操作).设计模式(一定要烂熟于心) 4.访 ...

  10. Emmet 的使用

    Emmet 的介绍 Emmet 的前身叫做:Zen Coding,也许熟知旧名的人不在少数.Emmet 一般前端工程师用得比较多,具体它是做什么的,我们通过下面两张 Gif 演示图来说明: Intel ...