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

以下是代码:

  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <cstdlib>
  5. using namespace std;
  6. int main(){
  7. int n,tn;
  8. int news,s,e,t;
  9. int maxn,sum;
  10. cin>> n;
  11. for(int i=1;i<=n;i++){
  12. sum = 0;
  13. maxn = -20000;
  14. news = s = e = 1;
  15. scanf("%d",&tn);
  16. for(int j=1;j<=tn;j++){
  17. scanf("%d",&t);
  18. sum+= t ;
  19. if(sum > maxn){ //目前的和大于记录和,更新和及起点终点
  20. maxn = sum;
  21. e = j;
  22. s = news;
  23. }
  24. if(sum < 0){//目前的和小于0,则将起点定为下一个数,和为0
  25. sum=0;
  26. news=j+1;
  27. }
  28. }
  29. printf("Case %d:\n%d %d %d\n",i,maxn,s,e);
  30. if(i!=n)printf("\n");
  31. }
  32. }

  

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. python3----输出所有大小写字母及数字

    1. 用一行输出所有大(小)写字母,以及数字 print([chr(i) for i in range(65, 91)]) # 所有大写字母 print([chr(i) for i in range( ...

  2. 编程之美 set 14 小飞的电梯调度算法

    题目 电梯每次上升只停一次, 求问电梯停在哪一楼能够保证乘坐电梯的所有乘客爬楼层的层数之和最小 思路 假设电梯的层数是 m, 乘客人数是 n 1. 枚举, 时间复杂度是 o(mn) 2. 滚动解法. ...

  3. 《Windows核心编程》读书笔记.Chapter06线程基础

    原文链接在印象笔记(效果也好的多):https://app.yinxiang.com/l/AAQlNLnxTPRMAppVr5W0upchipQDDC_FHlU 概要: 现成也有两个组成部分: 现成的 ...

  4. Zabbix-3.0.3实现微信(WeChat)告警

    导读 Zabbix可以通过多种方式把告警信息发送到指定人,常用的有邮件,短信报警方式,但是越来越多的企业开始使用zabbix结合微信作为主要的告警方式,这样可以及时有效的把告警信息推送到接收人,方便告 ...

  5. 【BZOJ1935/4822】[Shoi2007]Tree 园丁的烦恼/[Cqoi2017]老C的任务 树状数组

    题意:两道题差不多,都是给你一堆平面上的点,每个点有权值,然后m次询问求某一矩形区域内的点权和 题解:先离散化,然后将询问拆成左右两条线段,然后将点和这些线段一起按x坐标排序,在y轴上维护树状数组.然 ...

  6. js i++ 与 ++i 的区别

    转载:http://blog.csdn.net/hjb2722404/article/details/50423804 在javascript中,我们常常搞不懂i++与++i的区别,今天我们就来简单说 ...

  7. Hibernate中的映射关系(一对多)

    在数据库中表和表之间的关系有几种,(一对一,一对多,多对多)一对一关系:可以选择任意一方插入外键(one-to-one:one-to-one<--->many-to-one[unique= ...

  8. R语言中基于聚类的离群点挖掘

    思路:首先,通过K-means算法将数据点划分为成若K个簇:然后计算每一个数据对象到最近簇的中心距离,来与离群点设置的阈值进行比较,以此来判别该数据对象是否是离群点. 1.读取数据 data<- ...

  9. /usr/local/nginx/sbin/nginx -s reload 失败原因pid 进程记录和当前不符

    [root@a ~]# /usr/local/nginx/sbin/nginx -s reload;nginx: [alert] kill(18834, 1) failed (3: No such p ...

  10. golang 发送多人邮件 textproto.Error{Code:554, Msg:"Transaction failed: Illegal semicolon, not in group"

    网上很多版本发送邮件都是用; 号,关键在于,多个邮件分割不能用; 号,需要用,号 // send mail func SendMail(subject string, message string, ...