杭电ACM OJ 1003 Max Sum 一点点的动态规划思想 入门级
来源:互联网 发布:淘宝培训课程列表 编辑:程序博客网 时间:2024/06/01 13:43
Max Sum
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 260585 Accepted Submission(s): 61936
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翻译:就是几个int数,按序拜访 如6 -1 5 4 -7 找到其中连续的几个数 使得和为最大(不一定要从第一个数头开始)
第一个数是代表这个数组有几个 后面是具体的数
public class MaxSum1003 { private static List<Integer> list; private static int begin = 0; private static int end = 0; private static int max = 0; public static void main(final String[] args) throws Exception { initData(); calculate(list); System.out.println("" + max + (begin+1) + (end+1)); } private static void initData() { list = new ArrayList<>(); list.add(6); list.add(-1); list.add(5); list.add(4); list.add(-7); } private static void calculate(List<Integer> list) { int size = list.size(); int sum = 0; //如果固定从头开始,直接开始累加,不断和最大值比较// for (int i = 0; i < size; i++) {// sum += list.get(i);// max = Math.max(sum, max);// } //如果不一定要从头开始,就是本题情况,从一串数中,截取一段,需要是和最大 //这里和上面唯一的区别是需要加个if来判断,是否舍弃前面的东西 for (int i = 0; i < size; i++) { sum += list.get(i); if (sum > max) { max = sum; end = i; } //如果前面的sum和小于0了,还要他干什么,所以舍弃 if (sum < 0) { sum = 0; begin = i; } } }}
阅读全文
0 0
- 杭电ACM OJ 1003 Max Sum 一点点的动态规划思想 入门级
- 杭电acm第1003题Max Sum ( 动态规划)
- 杭电ACM OJ 1024 Max Sum Plus Plus 动态规划 二维dp+滚动数组dp优化
- 杭电OJ——1024 Max Sum Plus Plus(另类的动态规划!)
- 杭电OJ(HDOJ)1003题:Max Sum(动态规划)
- HDU OJ 1003 Max Sum 【动态规划】
- 杭电acm上有关动态规划思想的习题
- HDOJ 1003 Max Sum 动态规划入门(
- 杭电oj-1003-Max Sum
- 【杭电-oj】-1003-Max Sum
- 杭电OJ 1003 Max Sum
- 杭电acm 1003Max Sum
- HDOJ 1003 Max Sum 杭电 ACM
- 杭电ACM 1003 :Max Sum
- 杭电ACM 1003 Max Sum
- 杭电ACM-1003-Max Sum
- 杭电ACM 1003:Max Sum
- 杭电 HDU ACM 1003 Max Sum
- 简单线性回归
- Maven的基本使用
- 【技术分享】黑盒渗透测试的一些姿势和个人总结
- 简图记录-linux内存管理
- babyos2(0)——从零开始
- 杭电ACM OJ 1003 Max Sum 一点点的动态规划思想 入门级
- hdu1002(超超长数字相加)
- C++20171110日志 Static类型的声明
- 为什么表单中用name而不是id
- android4.4透明标题栏实现
- 使用DeviceIoControl读写磁盘MBR, 修改分区类型示例
- Spring 的微内核与FactoryBean扩展机制--转载
- 开源网站
- 我的Qt学习之路——按钮特效