码迷,mamicode.com
首页 > 编程语言 > 详细

HDU1024 Max Sum Plus Plus —— DP + 滚动数组

时间:2017-10-02 17:50:41      阅读:260      评论:0      收藏:0      [点我收藏+]

标签:algo   and   tor   pen   view   isp   fun   open   class   

题目链接:http://acm.split.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): 31798    Accepted Submission(s): 11278


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.
 

 

Author
JGShining(极光炫影)
 
 
 
题解:
 
 
 
代码如下:
技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <vector>
 7 #include <queue>
 8 #include <stack>
 9 #include <map>
10 #include <string>
11 #include <set>
12 #define ms(a,b) memset((a),(b),sizeof((a)))
13 using namespace std;
14 typedef long long LL;
15 const double EPS = 1e-8;
16 const int INF = 2e9;
17 const LL LNF = 2e18;
18 const int MAXN = 1e6+10;
19 
20 int a[MAXN];
21 int last_max[MAXN], all_max[2][MAXN];
22 
23 int main()
24 {
25     int n, m;
26     while(scanf("%d%d",&m, &n)!=EOF)
27     {
28         for(int i = 1; i<=n; i++)
29             scanf("%d", &a[i]);
30 
31         ms(all_max, 0);
32         ms(last_max, 0);
33 
34         int id = 0;
35         for(int i = 1; i<=m; i++)
36         {
37             id = !id;
38             all_max[id][i-1] = -INF;
39             for(int j = i; j<=n; j++)
40             {
41                 last_max[j] = max(last_max[j-1], all_max[!id][j-1]) + a[j];
42                 all_max[id][j] = max(all_max[id][j-1], last_max[j]);
43             }
44         }
45         printf("%d\n", all_max[id][n]);
46     }
47     return 0;
48 }
View Code

 

HDU1024 Max Sum Plus Plus —— DP + 滚动数组

标签:algo   and   tor   pen   view   isp   fun   open   class   

原文地址:http://www.cnblogs.com/DOLFAMINGO/p/7620385.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!