K - Max Sum Plus Plus

K - Max Sum Plus Plus

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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 S 1, S 2, S 3, S 4 ... S x, ... S n (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ S x ≤ 32767). We define a function sum(i, j) = S i + ... + S j (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(i 1, j 1) + sum(i 2, j 2) + sum(i 3, j 3) + ... + sum(im, j m) maximal (i x ≤ i y ≤ j x or i x ≤ j y ≤ j x 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(i x, j x)(1 ≤ x ≤ m) instead. ^_^

Input

Each test case will begin with two integers m and n, followed by n integers S 1, S 2, S 3 ... S n
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.

//题意是:第一行 m ,n (n<=1000000) 两个整数,然后第二行 n 个数,求 m 段不相交连续序列最大和。

自己想了一阵,还是没有思路。。。这个题很好。

这篇博客写得十分详细

http://www.cnblogs.com/ACMan/archive/2012/08/09/2630038.html

dp[i][j]代表的状态是 i 段连续序列的最大和,并且最后一段一定包含 num[j]

所以写出状态转移方程 dp[i][j]=max{dp[i][j-1]+a[j],max{dp[i-1][t]}+a[j]} i-1=<t<j-1

dp[i][j-1]代表连续的状态,后面是不连续的状态,取最大值。

数据很大,只能用一维数组

不断更新状态,用一个数据 big 保存 i 段最大的和,最后直接输出,就是答案

436ms

 1 #include <stdio.h>
 2 #include <string.h>
 3
 4 #define inf 0x7ffffff
 5 #define MAXN 1000005
 6 int num[MAXN];
 7 int dp[MAXN];
 8 int pre[MAXN];
 9
10 int max(int a,int b)
11 {
12     return a>b? a:b;
13 }
14
15 int main()
16 {
17     int m,n;
18     int i,j,big;
19     while (scanf("%d%d",&m,&n)!=EOF)
20     {
21         for (i=1;i<=n;i++)
22         {
23             scanf("%d",&num[i]);
24             pre[i]=0;
25         }
26
27         pre[0]=0;
28         dp[0]=0;
29
30         for (i=1;i<=m;i++)
31         {
32             big=-inf;
33             for (j=i;j<=n;j++)
34             {
35                 dp[j]=max(dp[j-1],pre[j-1])+num[j];//连续的最大和,或者不连续的最大和
36                 pre[j-1]=big;      //保存 i - 1 段 最大和
37                 big=max(big,dp[j]);//保证是 i 段最大的和
38             }
39         }
40         printf("%d\n",big);
41     }
42     return 0;
43 }

时间: 2024-10-26 02:38:28

K - Max Sum Plus Plus的相关文章

Leetcode: Max Sum of Rectangle No Larger Than K

Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix such that its sum is no larger than k. Example: Given matrix = [ [1, 0, 1], [0, -2, 3] ] k = 2 The answer is 2. Because the sum of rectangle [[0, 1], [

【leetcode】363. Max Sum of Rectangle No Larger Than K

题目描述: Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix such that its sum is no larger than k. 解题思路: 根据题意,寻找二维数组中所有可以组成的矩形中面积不超过k的最大值,所以必须要求出可能组成的矩形的面积并与k比较求出最终结果.这里为了最终不超时,可以在一下方面进行优化: 1.设置一个数组比较当前列(或

[LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K

Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix such that its sum is no larger than k. Example: Given matrix = [ [1, 0, 1], [0, -2, 3] ] k = 2 The answer is 2. Because the sum of rectangle [[0, 1], [

363 Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K

Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix such that its sum is no larger than k.Example:Given matrix = [  [1,  0, 1],  [0, -2, 3]]k = 2The answer is 2. Because the sum of rectangle [[0, 1], [-2

HDU 1003 Max Sum【动态规划求最大子序列和详解 】

Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 250714    Accepted Submission(s): 59365 Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max su

HDU 1024 Max Sum Plus Plus --- dp+滚动数组

HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值,其中第i个子序列包括a[j], 则max(dp[m][k]),m<=k<=n 即为所求的结果 <2>初始状态: dp[i][0] = 0, dp[0][j] = 0; <3>状态转移: 决策:a[j]自己成为一个子段,还是接在前面一个子段的后面 方程: a[j]直接接在前面

[2016-03-28][HDU][1024][Max Sum Plus Plus]

时间:2016-03-28 17:45:33 星期一 题目编号:[2016-03-28][HDU][1024][Max Sum Plus Plus] 题目大意:从n个数字提取出一定数字组成m个部分,使得这个部分的总和最大 分析: dp[i][j]表示前i段计算第j个数字,dp[i][j] = max(dp[i - 1][j - 1] + a[j],dp[i][k] + a[j]); #include <algorithm> #include <cstring> #include &

HDU 1024 Max Sum Plus Plus

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1024 Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21926    Accepted Submission(s): 7342 Problem Description Now I think you ha

Hdoj 1024 Max Sum Plus Plus 【DP】

Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18861 Accepted Submission(s): 6205 Problem Description Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To b