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): 24618    Accepted Submission(s): 8461


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 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. ^_^
 

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
 

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1024

校赛的题,当时比赛的时候没怎么看这个题,惭愧啊。

题目的意思是说把那一串数字分解成m个互不相交的子串,求把这些子串和加起来的最大值。
好吧,我觉得我表达的不太清楚,放个翻译
http://www.sdutacm.org/sdutoj/showproblem.php?pid=3556&cid=1755

题意都理解了吧,最好能肉眼过一遍实例知道实例是怎么出来的(哎,看了好几遍才读懂,渣渣啊)

下面我来讲一下这个神DP,也是网上搜了一下题解,看QAQ的博客心里明白,但总感觉还差一点,所以搜了一下题解,好吧,题解讲的很详细。我懂了,沉淀了一下来写这篇博客。
这是道DP题,所以就有状态转移方程,那么怎么找呢,你会想到什么思路?(什么思路也没有=-=||)
设一个数组dp[i][j],代表在前i个数中分解成j段。
再设一个数组存一下输入进来的数列a[];
那么状态转移方程不难给出
dp[i][j]=max(dp[i-1][j],dp[1~i][j-1])+a[i];
我们来解读一下这个状态转移方程,dp[i-1][j],是第i个数和前面一组,并没有单独分一组,这样讲的话对这个状态转移方程就没有问题了吧,后面的是一样的道理,一看就懂。
当你用这个状态转移方程去做题的时候,你会惊讶的发现,超时。。。。。。。毕竟这个题的数据很大!#¥%……&*()。
所以我们要在这里优化一下算法,仔细研究方程,你会惊讶的发现dp[i][j]只和dp[][j]和dp[][j-1]有关,是不是很神奇,这样的话你会想到什么方法,。。。。,对没错,用两个一维数组去维护,然后。。。。。。。。。就过了。

上个代码?嗯不打了,上代码

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
long long dp[1000000];
long long maxn[1000000];
long long a[1000000];//坑了我好久,数组开小了,但是返回的错误是wrong answer......
#define inf 1000000000
//记录金巨的写法
//const long long inf 0x3f3f3f3f3f3f3f3fll;
int main()
{
    long long n,k;
    while(~scanf("%lld%lld",&k,&n))
    {
        memset(a,0,sizeof(a));
        memset(maxn,0,sizeof(maxn));
        memset(dp,0,sizeof(dp));
        int i;
        for(i=1;i<=n;i++)
        {
            scanf("%lld",&a[i]);
        }
        for(i=0;i<=k;i++)
        {
            dp[i]=maxn[i]=-inf;
        }
        maxn[0]=0;
        for(i=1;i<=n;i++)
        {
            for(int j=k;j>=1;j--)//一共k组
            {
                dp[j]=max(maxn[j-1],dp[j])+a[i];
                maxn[j]=max(maxn[j],dp[j]);
            }
        }
        cout<<maxn[k]<<endl;//这里不能输出dp[]数组
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值