hdu 1024 Max Sum Plus Plus

来源:互联网 发布:淘宝店铺类目 编辑:程序博客网 时间:2024/06/11 08:10

原题:

>                                     Max Sum Plus Plus

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)

Problem Description
Now I think you have got an AC in Ignatius.L’s “Max Sum” problem. To be a brave ACMer, we always challenge ourselves to more difficult problems. Now you are faced with a more difficult problem.

Given a consecutive number sequence S1, S2, S3, S4 … Sx, … Sn (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ Sx ≤ 32767). We define a function sum(i, j) = Si + … + Sj (1 ≤ i ≤ j ≤ n).

Now given an integer m (m > 0), your task is to find m pairs of i and j which make sum(i1, j1) + sum(i2, j2) + sum(i3, j3) + … + sum(im, jm) maximal (ix ≤ iy ≤ jx or ix ≤ jy ≤ jx is not allowed).

But I`m lazy, I don’t want to write a special-judge module, so you don’t have to output m pairs of i and j, just output the maximal summation of sum(ix, jx)(1 ≤ x ≤ m) instead. ^_^

Input
Each test case will begin with two integers m and n, followed by n integers S1, S2, S3 … Sn.
Process to the end of file.

Output
Output the maximal summation described above in one line.

Sample Input
1 3 1 2 3
2 6 -1 4 -2 3 -2 3

Sample Output
6
8

Hint

Huge input, scanf and dynamic programming is recommended.

原题链接:
http://hdu.hustoj.com/showproblem.php?pid=1024

题意:这题大概的意思是,给出n个数的序列,然后在里面找出m段出来,使得这m段的和最大,每一段可以是一个数字组成,也可以是几个数字组成,但这几个数字必须连续的,上面的Ouput是这样得出来的:
第一个:1 3 1 2 3,m=1,n=3,在这三个数中取一段出来,使其和最大,最大的一段就是1,2,3这段,和为6
第二个:2 6 -1 4 -2 3 -2 3 m=2,n=6,在这六个数中取两段出来,4,-2,3为一段,最后的数字3为一段,这两段的和最大,为8.

java代码:
这里参考了另外一篇博客写的,另外一篇博客是用C语言写的,那里有更详细的图解
http://blog.sina.com.cn/s/blog_677a3eb30100jxqa.html
这篇博客为了节省内存,采用了连滚数组

import java.util.Scanner;public class Main {    private static long[][] dp = new long[2][1000001];    private static long[] a = new long[1000001];    private static int n,m;    private static long maxNum;  //上一行中j之前最大的那个数    private static long res ;   //分成m段最大值,初始化long范围最小值    private static int index;   //两行数组索引下标    public static void main(String[] args) {        Scanner input = new Scanner(System.in);        while (input.hasNext()){            m = input.nextInt();            n = input.nextInt();            index = 1;            res = Long.MIN_VALUE;            /**             * dp[0][]和dp[1][]是相邻的两行,在处理数据中相互转换             */            for(int i=1;i<=n;i++){                dp[0][i] = dp[1][i] = 0;                a[i] = input.nextInt();            }            for(int i=1;i<=m;i++){                dp[index][i] = dp[1 - index][i-1] + a[i];                maxNum = dp[1 - index][i-1];                for (int j=i+1;j<=n-m+i ;j++){                    maxNum = Math.max(maxNum,dp[1-index][j-1]);                    dp[index][j] = Math.max(dp[index][j-1],maxNum) + a[j];                }                index = 1 - index;  //转换两行数据            }            index = 1 - index;            for (int j=m;j<=n;j++){                res = Math.max(dp[index][j],res);            }            System.out.println(res);        }    }}
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 六角螺帽拧花了怎么办 六角螺母拧花了怎么办 内六角螺丝滑丝怎么办 十字螺丝拧花了怎么办 梅花螺丝滑丝了怎么办 螺丝孔道滑丝了,怎么办 锅把铆钉松啦怎么办 膨胀螺丝孔大了怎么办 戒指不圆了怎么办妙招 眼镜框的腿断了怎么办 弹簧腿眼镜坏了怎么办 vps搭建ss被墙怎么办 注塑机螺杆黏料怎么办 分解师耐久度0了怎么办 有鼻涕擤不出来怎么办 擤鼻涕耳朵堵了怎么办 脸用什么都过敏怎么办 1岁宝宝流清鼻涕怎么办 2岁宝宝流清鼻涕怎么办 3岁儿童流清鼻涕怎么办 鼻子老是流清水鼻涕怎么办 5岁儿童感冒鼻塞怎么办 3岁宝宝感冒鼻塞怎么办 宝宝9个月流鼻涕怎么办 鼻涕又黄又粘稠怎么办 鼻子一直流黄水怎么办 宝宝眼屎多又黄怎么办 宝宝痰多鼻涕多怎么办 小孩痰多鼻涕多怎么办 用qq登不了微博怎么办 中国银行u盾丢了怎么办 我的世界遇见him怎么办 考军校年龄超了怎么办 dnf组队就红电脑怎么办 九阳高压锅漏气怎么办 晋江买了防盗章怎么办 开车撞了人应该怎么办 开车撞了人没钱怎么办 驾照扣分12分后怎么办 车被交警拖走了怎么办 符石耐久没了怎么办