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 ...
随机推荐
- 1.Java
面向对象:封装,继承,多态 A组合B:那么B就可以调用A中的方法 A关联B:A中的成员变量是用B声明的 A和B的依赖关系:A中某个方法的参数是B声明的对象或者返回值类型是B的数据类型 Static方法 ...
- related_name
定义表Apple: class Apple( models.Model): origin_level = models.ForeignKey(AppleLevel) new_level = model ...
- golang 统计uint64 数字二进制存储中1的数量
package main import ( "fmt") // pc[i] is the population count of i.var pc [256]byte fun ...
- docker:学习笔记
docker run -itd --net=none 22565cef72c2 /usr/sbin/sshd -Dpipework br0 5a3e7bab4c5c5260a93e153aa7fec3 ...
- HTTPS 基本流程 转载 https://zhuanlan.zhihu.com/p/27395037
协议 1.HTTP 协议(HyperText Transfer Protocol,超文本传输协议):是客户端浏览器或其他程序与Web服务器之间的应用层通信协议 . 2.HTTPS 协议(HyperTe ...
- mysql安装,以及从csv插入数据
1.mysql安装 用管理员身份打开cmd命令行工具,cd到解压文件的bin目录: ・MySql的Windows服务安装: mysqld install 回车 ・生成无密码的root用户: my ...
- 一个request引发的bug
有很多错误由于需要是多线程是才会发生,导致经常在开发时很难发现, import java.lang.reflect.ParameterizedType; import java.util.List; ...
- [Flutter] 写第一个 Flutter app,part1 要点
模拟器中调试元素的布局: Android Studio 右侧边栏 Flutter Inspector,选择 Toggle Debug Paint 打开. 格式化代码: 编辑器中右键 Reformat ...
- 【转】WPS word 文档中的插入对象 为什么打不开
点击桌面左下角开始按钮--所有程序,找到wps office文件夹--wps office工具--配置工具--高级--兼容设置,否选兼容第三方软件.
- Bootstrap中的data-toggle,data-target
data-toggle指以什么事件触发常用的如collapse,modal,popover,tooltips等:data-target指事件的目标, 一起使用就是代表data-target所指的元素以 ...