7A - Max Sum
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的更多相关文章
- [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 ...
- 2016huasacm暑假集训训练五 J - Max Sum
题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/J 题意:求一段子的连续最大和,只要每个数都大于0 那么就会一直增加,所以只要和0 ...
- 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 ...
- HDU 1024 max sum plus
A - Max Sum Plus Plus Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
- hdu 1024 Max Sum Plus Plus
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行
测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...
- 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 ...
- hdu 1003 Max sum(简单DP)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem ...
- HDU 1003 Max Sum
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
随机推荐
- 高性能 TCP & HTTP 通信框架 HP-Socket v4.2.1
HP-Socket 是一套通用的高性能 TCP/UDP/HTTP 通信框架,包含服务端组件.客户端组件和 Agent 组件,广泛适用于各种不同应用场景的 TCP/UDP/HTTP 通信系统,提供 C/ ...
- vs关于“当前不会命中断点 还没有为该文档加载任何符号”的解决方法
首先调式的时候确定在debug模式下, 解决方法:工具-选项-调试 -(启用“仅我的代码”)勾去掉.
- Postman Could not get any response
在使用postman时遇到的小问题,记录一下: 报错信息如下: Could not get any response There was an error connecting to https:// ...
- IE8图片上传预览
$("#smallImg").attr('style', "filter:progid:DXImageTransform.Microsoft.AlphaImageLoad ...
- C语言博客作业3--函数
C语言博客作业3--函数 1.本章学习总结 1.1思维导图 请以思维导图总结本周的学习内容,如下图所示: 1.2本章学习体会及代码量学习体会 1.2.1学习体会 描述本周学习感受,也可以在这里提出你不 ...
- Hexo:创建属于你自己的博客
step: 1.install node.js,git,github 2.npm install -g hexo-cli 3.mkdir hexo 4.cd hexo mkdir blog 5.cd ...
- DLC 复合逻辑运算
与非逻辑运算 或非逻辑运算 与或非逻辑运算 异或逻辑运算 同或逻辑运算
- dubbo rest服务 No provider available for the service 错误问题
1.版本 dubbo 2.6.2 2.描述 消费者调用dubbo rest服务报No provider available for the service错误 网络上有讲是实体类未实现Serializ ...
- springboot + schedule
参考文章:https://blog.csdn.net/sinianliushui/article/details/78841713 参考文章: https://blog.csdn.net/hao703 ...
- 一文让你秒懂互联网TCP/IP协议的深层含义
什么是 TCP/IP 协议 首先,协议,可以理解为是一套统一的规则,就像行业标准.由于互联网主要的功能是传输信息,所以其协议一般是管理系统之间如何相互通信的规则. 用邮政和物流等线下的“运输协议”来理 ...