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. 基础总结(04)-- display:none;&&visibility:hidden;区别

    display:none 1.使元素隐藏,不再占据空间. 2.动态操作时会引起页面回流和重绘,影响性能. 3.子元素也会被隐藏并且添加display:block/visibility:visible无 ...

  2. JAVA 数组遍历

    一.遍历List 1.增强for循环 String[] arr = new String[] {"xx","yy","zz"}; for(S ...

  3. Java2E中的路径问题

    本节主要介绍: 1.request.getContextPath()-----项目的发布的根路径 2.request.getRealPath('t')----t目录在当前磁盘中的物理位置,包括盘符,文 ...

  4. OpenCL 矩阵乘法

    ▶ 矩阵乘法,按照书里的内容进行了几方面的优化,包括局部内存,矢量数据类型,寄存器,流水线等. ● 最直接的乘法.调用时 main.c 中使用 size_t globalSize[] = { rowA ...

  5. 学习Flask框架

      # -*- encoding: utf-8 -*- #导包 from flask import Flask #建立flask对象 app = Flask(__name__) #使用flask路由器 ...

  6. sqlserver 存储过程 自定义函数 游标???

    create proc cur_fun( @cur cursor --输入参数 ) as begin declare @mytpye tb1_type ) fetch next from @cur i ...

  7. Faiss in python and GPU报错:NotImplementedError: Wrong number or type of arguments for overloaded function 'new_GpuIndexFlatL2'.

    最近在玩faiss,运行这段代码的时候报错了: res = faiss.StandardGpuResources()flat_config = 0index = faiss.GpuIndexFlatL ...

  8. python基础1.0

    1. python简介:解释性语言 安装python,注意路径加入path python的解释器,cpython,Python的解释器很多,但使用最广泛的还是CPython.如果要和Java或.Net ...

  9. Servlet学习记录4

    带进度条的文件上传 UploadServlet只实现了普通的文件上传,并附带普通文本域的提交.如果需要显示上传进度条,实时显示上传速度等,需要配合使用Ajax技术.这里仍然使用Apache的commo ...

  10. php使用coreseek进行中文分词搜索

    方法一 使用coreseek源码自带testpack/api/test_coreseek.php代码,进行稍微修改就可以使用了,只不过需要引入”spinxapi.php“类 方法二--制作php扩展 ...