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. mac os x 记录 转载

    转载:远景网友(手机锋友t5sd3sf):http://bbs.feng.com/read-htm-tid-10434256.html 一个命令制作 OS X 原版安装U盘 1.要保证下载的原版安装包 ...

  2. PHP的只是结构图

  3. keyword static

    1. 不能通过类名来调用类的非静态成员函数 2. 类的对象可以使用静态成员函数和非静态成员函数 3. 静态成员函数中不能引用非静态成员 因为静态成员函数属于整个类, 在类的实例化对象之前就已经分配了空 ...

  4. 使用PHP,jsonp,jquery实现跨域

    html代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  5. 160307、Java调用Oracle存储过程返回结果集

    一:无返回值的存储过程调用 存储过程: CREATE OR REPLACE PROCEDURE PRO_1(PARA1 IN VARCHAR2,PARA2 IN VARCHAR2)   AS BEGI ...

  6. distinct用group by优化

    当数据量非常大,在同一个query中计算多个不相关列的distinct时,往往很容易出现数据倾斜现象,导致运行半天都不能得到结果. 比如以下的SQL语句(a, b, c没有相关性): select d ...

  7. c# winfrom实时获取斗鱼房间弹幕

    效果图如下: 通过webBrowser获取,时钟控件刷新弹幕,正则匹配数据,用第二个webBrowser显示弹幕内容.老话,并没完善.请自行完善.有个dll是用来屏蔽webBrowser的声音的,可能 ...

  8. TIME_WAIT Accumulation and Port Exhaustion

    客户端实现连接的唯一性 HTTP The Definitive Guide 4.2.7 TIME_WAIT Accumulation and Port Exhaustion TIME_WAIT por ...

  9. [已解决]ubuntu下chrome和firefox输入框内无法快捷键全选

    问题现象: 在chrome或firefox浏览器(其他地方没试)的输入框中使用ctr + a进行全选失效,在google中找到了这个已经解决的http://askubuntu.com/question ...

  10. Sparrow - Distributed, Low Latency Scheduling

    http://www.cs.berkeley.edu/~matei/papers/2013/sosp_sparrow.pdf http://www.eecs.berkeley.edu/~keo/tal ...