博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Max Sum
阅读量:5315 次
发布时间:2019-06-14

本文共 1841 字,大约阅读时间需要 6 分钟。

Problem 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 
2 3 int main(){ 4 int T; 5 int n; 6 int number[100001]; 7 int i; 8 int sum; 9 int max;10 int start;11 int end;12 int time;13 int temp;14 15 16 scanf("%d",&T);17 time=1;18 19 while(T--){20 sum=0;21 max=-1000;22 start=0;23 end=0;24 temp=0;25 26 scanf("%d",&n);27 28 for(i=0;i
max){35 max=sum;36 start=temp;37 end=i;38 }39 40 if(sum<0){ //关键是这里,当求和小于0时,便把下一个数值作为开头再找最大值41 sum=0;42 temp=i+1;43 }44 }45 46 printf("Case %d:\n",time);47 time++;48 printf("%d %d %d\n",max,start+1,end+1);49 if(T!=0)50 printf("\n");51 }52 53 return 0;54 }

 

转载于:https://www.cnblogs.com/zqxLonely/p/4062348.html

你可能感兴趣的文章
各种正则验证
查看>>
观察者模式(Observer)
查看>>
python中numpy.r_和numpy.c_
查看>>
大道至简阅读笔记02
查看>>
WPF简单模拟QQ登录背景动画
查看>>
bzoj 2038 小Z的袜子
查看>>
egret3D与2D混合开发,画布尺寸不一致的问题
查看>>
freebsd 实现 tab 命令 补全 命令 提示
查看>>
numpy调试
查看>>
struts1和struts2的区别
查看>>
函数之匿名函数
查看>>
shell习题第16题:查用户
查看>>
实验4 [bx]和loop的使用
查看>>
Redis常用命令
查看>>
2018.11.06 bzoj1040: [ZJOI2008]骑士(树形dp)
查看>>
2019.02.15 bzoj5210: 最大连通子块和(链分治+ddp)
查看>>
redis cluster 集群资料
查看>>
混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。...
查看>>
微软职位内部推荐-Sr. SE - Office incubation
查看>>
微软职位内部推荐-SOFTWARE ENGINEER II
查看>>