1002 Max Sum
来源:互联网 发布:算法导论英文第四版pdf 编辑:程序博客网 时间:2024/06/08 11:14
Max Sum
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
25 6 -1 5 4 -77 0 6 -1 1 -6 7 -5
Sample Output
Case 1:14 1 4Case 2:7 1 6
Author
Ignatius.L
先顺序求最大值 找到最大的和 标记末值。再反过来从末值减回去到sum变回0,标记初值。
#include <stdio.h>int main(){ int t; scanf("%d",&t); for(int v=1;v<=t;v++) { if(v!=1)printf("\n"); int n,a[100100]={0}; scanf("%d",&n); int sum=0,max=-2000,begin=1,end=1; for(int i=1;i<=n;i++) { scanf("%d",&a[i]); sum=sum+a[i]; if(sum>max)max=sum,end=i; if(sum<0)sum=0; }//求出最大值和末值 sum=max; for(int i=end;i>0;i--) { sum=sum-a[i]; if(sum==0)begin=i; } printf("Case %d:\n",v); printf("%d %d %d\n",max,begin,end); } return 0;}
0 0
- 1002 Max Sum
- Max Sum
- Max Sum
- Max Sum
- max sum
- Max Sum
- Max Sum
- MAX SUM
- Max Sum
- Max Sum
- max sum
- Max Sum
- Max sum
- Max Sum
- Max sum
- Max Sum
- Max Sum
- Max Sum
- 学习pandas下的dataframe画图参数
- Nginx支持Socket转发过程详解
- SSO CAS+Shiro+springmvc单点登录解决方案
- android中处理 Unable to resolve target 'android-4'的问题
- Spark SQL 算子实例
- 1002 Max Sum
- IPv6地址格式及子网划分方法
- MMU地址映射过程
- READING NOTE: Understanding Convolution for Semantic Segmentation
- python学习[4]_利用numpy做一些距离的计算
- List,ArrayList, LinkedList类的区别
- android 给assets文件夹中图片设置点击、按下状态
- JVM垃圾收集相关常用参数
- java static关键字的使用