Max Sum Plus Plus

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(i  m, 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. ^_^ 

InputEach test case will begin with two integers m and n, followed by n integers S 1, S2, S 3 ... S n
Process to the end of file. 
OutputOutput 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.

 

 

最近这段时间来复习基础DP  必须强化一下DP了  

什么DP都不会  我好绝望啊

 

这个题目 我是看题解写的  ,慢慢来  。

总有一天我能够自己写DP的。

题意是一个长度为n的数组  分成不想交的m个字段

求出子段和最大值的问题

这题很明显的是一个DP

我写的代码是参考  kuangbin 大神的

解析很详细。

这个代码优化非常非常的强 

for (int i=1 ;i<=m ;i++ )  枚举子序列短,

for (int j=i ;j<=n ;j++)   因为对应的是第i个字段,所以最少前面要有 i 个元素

所以从 i 开 头 枚举

用 maxn 记录此时的

 

 每次进行枚举一个字段时候 都要讲maxn进行初始化

maxn 记录的是最大值 ,num[ j - 1 ] 记录上一次的最大值

maxn = max(dp[j], maxn);  这个就是用来比较相加后 值变大了 还是变小了。

以上就是我的理解 !  DP需要顿悟啊。

 
 1 #include <cstdio>
 2 #include <iostream>
 3 #include <set>
 4 #include <string>
 5 #include <cstring>
 6 #include <cstdio>
 7 #include <algorithm>
 8 #include <cmath>
 9 #include <queue>
10 #include <vector>
11 using namespace std;
12 
13 const int maxn = 1e6 + 10;
14 const int inf = 1e9 + 7;
15 
16 int dp[maxn], num[maxn], a[maxn];
17 int n, m;
18 
19 int main() {
20     while(scanf("%d%d", &m, &n) != EOF) {
21         for (int i = 1 ; i <= n ; i++ ) {
22             scanf("%d", &a[i]);
23             dp[i] = 0;
24             num[i] = 0;
25         }
26         dp[0] = 0;
27         num[0] = 0;
28         int maxn;
29         for (int i = 1 ; i <= m ; i++ ) {
30             maxn = -inf;
31             for (int j = i ; j <= n ; j++) {
32                 dp[j] = max(dp[j - 1], num[j - 1]) + a[j];
33                 num[j - 1] = maxn;
34                 maxn = max(dp[j], maxn);
35             }
36         }
37         printf("%d\n", maxn);
38     }
39     return 0;
40 }

 

转载于:https://www.cnblogs.com/qldabiaoge/p/8747705.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值