HDU-1003-Max Sum
来源:互联网 发布:京东分析与数据库 编辑:程序博客网 时间:2024/06/04 01:36
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 113689 Accepted Submission(s): 26291
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
import java.util.Scanner;public class HDU_1003_Max_Sum {/** * Accepted1003421MS3636K966 BJava1983210400 */public static void main(String[] args) { int max,sum,A,B,a,b,t; Scanner scan=new Scanner(System.in); int N=scan.nextInt(); for(int i=1;i<=N;i++){ int M = scan.nextInt(); A=B=a=b=1; max=sum=-1001;; for(int j= 1; j<=M ; j++){ t=scan.nextInt();if(sum + t < t){ sum = t; a=b=j; //每一次读入t时与sum+t,若小于读入的值,换个位置,再从新读取} else {sum += t ; ++b;//若大于读入的值,正好,那就最大连续串和序列的末尾位置b需+1,即(++b),同时将新的成员加入sum } if(max<sum){max=sum;A=a;B=b;//大于max时再改变其值,起始位置,终止位置} }System.out.println("Case "+i+":");//换行,循序渐进! System.out.println(max+" "+A+" "+B);if(i!=N) System.out.println(); /*System.out.println("Case "+i+":\n"+max+" "+A+" "+B);//PE,这样的格式输出不对!!切记!,虽然看着对! if(i!=N) System.out.println();*/ }}}
C语言版:
#include <stdio.h> int main() { int z,n,max,sum; int a,b,A,B,t; scanf("%d",&z); for(int k=1;k<=z;k++) { scanf("%d",&n); sum = max = -1001; for(int i=1;i<=n;i++) { scanf("%d",&t); if(sum+t < t) sum = t , a = b = i; //a、b记录当前连续子序列的起始、结束位置 else sum += t , ++b; if(max < sum) max = sum , A = a , B = b; } printf("Case %d:\n%d %d %d\n",k,max,A,B); if(k-z) puts(""); } return 0; }
- HDU 1003 Max Sum
- hdu 1003 Max Sum
- hdu 1003 Max Sum
- HDU-1003 max sum
- HDU 1003 - Max Sum
- HDU 1003 Max Sum
- hdu 1003 Max Sum
- HDU 1003 Max Sum
- HDU 1003 Max Sum
- hdu 1003 max sum
- HDU 1003 Max Sum
- hdu 1003 Max Sum
- HDU 1003 Max Sum
- hdu 1003 Max Sum
- HDU 1003 Max Sum
- Hdu 1003 - Max Sum
- HDU-1003-Max Sum
- hdu - 1003 - Max Sum
- 开始写博客
- EOS 流程走的最后一步时常出现的错误
- static_cast、dynamic_cast reinterpret_cast和const_cast
- Android中Ringtone播放详解【安卓源码解析五】
- javap反编译命令的使用
- HDU-1003-Max Sum
- vlc学习计划(6~~9)
- Wikioi 天梯 Cantor表(1083)
- 2 括号配对问题
- TortoiseSVN 客户端 保存密码
- Android中launcherMode="singleTask"详解【android源码解析六】
- hadoop 一步一步安装成功
- HDU 4618 Palindrome Sub-Array 二分(奇偶)+hash判断是否对称
- 鼠标右键禁用js方法