Time Limit:1000MS

Memory Limit:32768KB

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>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main(){
int n,tn;
int news,s,e,t;
int maxn,sum;
cin>> n;
for(int i=1;i<=n;i++){
sum = 0;
maxn = -20000;
news = s = e = 1;
scanf("%d",&tn);
for(int j=1;j<=tn;j++){
scanf("%d",&t);
sum+= t ;
if(sum > maxn){ //目前的和大于记录和,更新和及起点终点
maxn = sum;
e = j;
s = news;
}
if(sum < 0){//目前的和小于0,则将起点定为下一个数,和为0
sum=0;
news=j+1;
}
}
printf("Case %d:\n%d %d %d\n",i,maxn,s,e);
if(i!=n)printf("\n");
}
}

  

Winter-1-D Max Sum 解题报告及测试数据的更多相关文章

  1. HDU 1003 Max Sum 解题报告

    题目大意:求一串数字中,几个连续数字加起来最大值,并确定起始和最末的位置. 思路:这是一题DP题,但是可以用尺取法来做.我一开始不会,也是看了某大神的代码,然后有人告诉我这是尺取法,现在会了. //尺 ...

  2. HOJ 1003 Max Sum 解题报告

    好几年没有做ACM了,感觉忘得差不多了,这个做着做着就打瞌睡了!言归正传,下面是我的解题思路: 首先的话,我们可以画一个函数图,以输入数组的下标为X轴,以数组的和为Y轴,当数组和小于零时,我们使用备用 ...

  3. Winter-1-B Sum 解题报告及测试数据

    Time Limit:500MS Memory Limit:32768KB Description ​Hey, welcome to HDOJ(Hangzhou Dianzi University O ...

  4. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  5. LeetCode 2 Add Two Sum 解题报告

    LeetCode 2 Add Two Sum 解题报告 LeetCode第二题 Add Two Sum 首先我们看题目要求: You are given two linked lists repres ...

  6. LeetCode: Combination Sum 解题报告

    Combination Sum Combination Sum Total Accepted: 25850 Total Submissions: 96391 My Submissions Questi ...

  7. USACO Section2.3 Zero Sum 解题报告 【icedream61】

    zerosum解题报告----------------------------------------------------------------------------------------- ...

  8. 【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  9. 【LeetCode】508. Most Frequent Subtree Sum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. Ubuntu 16.04 LTS 安装libvips出现”Package vips was not found in the pkg-config search path”

    使用libvips来操作图像,libvips的部署参考一个Node.js工程:https://github.com/lovell/sharp 在MAC下安装很顺利,到Linux环境下(Ubuntu 1 ...

  2. JavaScript 事件循环 — event loop

    引言 相信所有学过 JavaScript 都知道它是一门单线程的语言,这也就意味着 JS 无法进行多线程编程,但是 JS 当中却有着无处不在的异步概念 .在初期许多人会把异步理解成类似多线程的编程模式 ...

  3. videoview 播放视频

    -videoVIew 继承SurfaceView 使用android的VideoView来播放一个视频,步骤是: 1,在xml中创建一个videoView, 2,在java中导入,然后创建一个Medi ...

  4. TCP连接的建立与终止过程详解

    TCP连接的建立与终止: 1.TCP连接的建立      设主机B运行一个服务器进程,它先发出一个被动打开命令,告诉它的TCP要准备接收客户进程的连续请求,然后服务进程就处于听的状态.不断检测是否有客 ...

  5. 『SharePoint 2010』Sharepoint 2010 Form 身份认证的实现(基于SQL)

    1:创建一个基于身份认证的应用程序(具体参见上篇基于AD) SQL-MembershipProvider 成员SQL-RoleManager 角色 2:修改管理中心,我们创建的应用程序,还有Web服务 ...

  6. C++编译遇到参数错误(cannot convert parameter * from 'const char [**]' to 'LPCWSTR')

    转:http://blog.sina.com.cn/s/blog_9ffcd5dc01014nw9.html 前面的几天一直都在复习着被实习落下的C++基础知识.今天在复习着上次创建的窗口程序时,出现 ...

  7. angular4 在页面跳转的时候传递多个参数到新页面

    页面跳转 router.navigate //单一参数: this.router.navigate(['/detail',id]); //多个参数: this.router.navigate(['/d ...

  8. Servlet------>jsp jstl核心标签库

    这里不需要刻意记,在jar里,c.tld文件都有,可以自己找源码看

  9. C#/java 执行oracle package

    使用pl/sql创建package CREATE OR REPLACE PACKAGE FirstPage is type outlist is ref cursor; Procedure p_get ...

  10. qt model view 编程总结

    看不见的root的 QModelIndex() 是 无效的 list 和table 的index 函数中的parent参数就只要 root QModelIndex 就可以,因为没有层级概念 Model ...