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 // WA*9,Time Limit Exceeded*2
代码省略
// 当需要函数返回多个值时,可使用结构类型
// 分治法(详见紫书8.1.3)(注意点见代码)
 #include<stdio.h>

 struct Subsq
{ int sum; int l; int r; }; struct Subsq max_sum(int a[], int left, int right) // 在区间[left,right)寻找最大连续和
{
struct Subsq b, leftsq, rightsq; // 最优解要么全在左半边,要么全在右半边,要么起点在左半边、终点在右半边
int mid, i;
b.l=left; b.r=right;
if(b.r-b.l==) b.sum=a[b.l]; // 若只有一个元素,则返回它
else
{
mid=b.l+(b.r-b.l)/;
leftsq=max_sum(a,b.l,mid); rightsq=max_sum(a,mid,b.r);
int sum1=a[mid-], sum2=a[mid], ls=, rs=, l=mid-, r=mid+; // 起点在中间,分别向左、右推进
for(i=mid-;i>=b.l;i--)
{
ls+=a[i];
if(ls>=sum1)
{ sum1=ls; l=i; }
}
for(i=mid;i<b.r;i++)
{
rs+=a[i];
if(rs>sum2)
{ sum2=rs; r=i+; }
}
b.sum=sum1+sum2; b.l=l; b.r=r; // 记录起点在左半边、终点在右半边情况下的最大连续和
if(b.sum<=leftsq.sum) // If there are more than one result, output the first one.
{ b.sum=leftsq.sum; b.l=leftsq.l; b.r=leftsq.r; }
if(b.sum<rightsq.sum)
{ b.sum=rightsq.sum; b.l=rightsq.l; b.r=rightsq.r; }
}
return b;
} int main()
{
struct Subsq b;
int t, n, a[], i, j;
scanf("%d", &t);
for(j=;j<=t;j++)
{
scanf("%d", &n);
for(i=;i<n;i++)
scanf("%d", &a[i]);
b=max_sum(a,,n);
printf("Case %d:\n%d %d %d\n", j, b.sum, b.l+, b.r);
if(j<t) printf("\n");
}
return ;
}

AC

// 补充:最大连续和问题(详见紫书8.1)

7A - Max Sum的更多相关文章

  1. [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K

    Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...

  2. 2016huasacm暑假集训训练五 J - Max Sum

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/J 题意:求一段子的连续最大和,只要每个数都大于0 那么就会一直增加,所以只要和0 ...

  3. Max Sum

    Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub ...

  4. HDU 1024 max sum plus

    A - Max Sum Plus Plus Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  5. hdu 1024 Max Sum Plus Plus

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

  6. hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行

    测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...

  7. Max Sum Plus Plus——A

    A. Max Sum Plus Plus Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To ...

  8. hdu 1003 Max sum(简单DP)

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

  9. HDU 1003 Max Sum

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

随机推荐

  1. scrapy使用指南

    创建scrapy项目: scrapy startproject 项目名 cd到项目名下 scrapy genspider 爬虫名 www.baidu.com(网站网址) 之后按照提示创建爬虫文件(官方 ...

  2. 创建windoes 硬盘 挂载u盘

    一,创建 windows 硬盘 创建 2,我的电脑右击鼠标,计算机管理 3.同理执行,在linux 下创建虚拟磁盘 选择路径为之前存放 windows 系统路径 之后 mount  /dev/sdc ...

  3. 熟悉Junit单元测试方法

    定义: JUnit是一个Java语言的单元测试框架.它由Kent Beck和Erich Gamma建立,逐渐成为源于Kent Beck的sUnit的xUnit家族中最为成功的一个. JUnit有它自己 ...

  4. V-REP Remote API(C++)实现简单的关节转动

    基础内容参考:https://www.cnblogs.com/eternalmoonbeam/p/10753149.html V-REP客户端设置: 在V-REP场景文件中需要添加三个实体,包括两个形 ...

  5. Linux 重装系统 连接不上的问题

    https://blog.csdn.net/liqi_q/article/details/78465949 ssh-keygen -R ip

  6. 5.SLB排错思路

    500/502/504可能的原因: https://help.aliyun.com/knowledge_detail/55207.html 请求不均衡可能的原因: https://help.aliyu ...

  7. 用python发送短消息(基于阿里云平台)

    新版短信接口在线测试页面:https://api.aliyun.com/new#/?product=Dysmsapi&api=SendSms&params={}&tab=DEM ...

  8. mysql 采样查询 / 间隔查询 / 跳跃查询的两种实现思路

    先创建一张测试表 CREATE TABLE `test` ( `id` ) DEFAULT NULL, `) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET= ...

  9. win10 开发mfc 64位 ocx控件

    问题1.模块“XXX.ocx”加载失败 解决办法:项目--〉属性--〉常规-〉配置类型-〉  动态库(.dll) 修改为 静态库(.lib) 问题2.1>x64\Release\stdafx.o ...

  10. centos7下部署mariadb+galera数据库高可用集群

    [root@node1 ~]# cat /etc/yum.repos.d/mariadb.repo # MariaDB 10.1 CentOS repository list - created 20 ...