HDU 1003 MAX SUM java 实现
来源:互联网 发布:ppt数据分析图表对比 编辑:程序博客网 时间:2024/05/22 14:14
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 141614 Accepted Submission(s): 32953
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/* * 典型动态规划的思想 动态规划的经典题 之前我一直不是很理解动态规划 * 我个人做题的时候 遇到动态规划的题型 直接用递归 * 算法分析:求最大字段和,d[i]表示在已i结尾(包含i)在a[1...i]上的最大和,d[i]=(d[i-1]+a[i]>)?d[i-1]+a[i]:a[i]; * max={d[i],n>=i>=1} * * */import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner in =new Scanner(System.in);final int N=100010; //注意定义多一些 开始我定义少了 提交三次都是WAint[] a =new int[N];int[] d=new int[N];int ee=0; int test=in.nextInt(); for(int j=0;j<test;j++){ int x=in.nextInt(); for(int y=1;y<=x;y++){ a[y]=in.nextInt(); } d[1]=a[1]; for(int m=2;m<=x;m++)// 动态规划的核心 { if(d[m-1]<0) d[m]=a[m]; else d[m]=d[m-1]+a[m]; } int max=d[1],e=1; for(int a1=2;a1<x+1;a1++)//找出最右边的点 { if(max<d[a1]) { max=d[a1]; e=a1; } } int t=0,f=e; for(int i=e;i>0;i--)//找出最左边的点 { t=t+a[i]; if(t==max) f=i; } System.out.println("Case "+(j+1)+":"); System.out.println(max+" "+f+" "+e); ee++; if(ee!=test) System.out.println(); }}}
0 0
- HDU 1003 MAX SUM java 实现
- HDU 1003 Max Sum Java
- hdu-1003(java)-Max Sum (DP)
- 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
- OCP 1Z0 053 172
- CSS中margin边界叠加问题及解决方案
- 11.4 AWT常用组件
- java中构造方法和普通方法的区别,以及和类的联系
- 如何下载dlx
- HDU 1003 MAX SUM java 实现
- Framework概述(一)
- Windows访问VMware Workstation中通过NAT联网的虚拟机
- 不小心删除了myeclipse下新建项目里面的J2EE 5 libraries 和 JRE System Library怎么恢复回去啊,急!
- HDOJ 2001 计算两点间的距离
- Unix/Linux环境C编程入门教程(27) 内存那些事儿
- 我的第一博客
- Windows与Linux(Ubuntu)双系统时间不一致的解决方法
- jquery 获得星期几