zoukankan      html  css  js  c++  java
  • HDU 1024:Max Sum Plus Plus(DP)

    Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 39886 Accepted Submission(s): 14338

    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(1xn1,000,000,32768Sx32767)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)=Si+...+Sj(1ijn)sum(i, j) = S_i + ... + S_j (1 ≤ i ≤ j ≤ n).

    Now given an integer m(m>0)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)sum(i_1, j_1) + sum(i_2, j_2) + sum(i_3, j_3) + ... + sum(i_m, j_m) maximal (ixiyjxi_x ≤ i_y ≤ j_x or ixjyjxi_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 ii and jj, just output the maximal summation of sum(ix,jx)(1xm)sum(i_x, j_x)(1 ≤ x ≤ m) instead.

    Input

    Each test case will begin with two integers mm and nn, followed by nn integers S1,S2,S3...SnS_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
    

    题意

    将一个长度为nn的数组分成不相交的mm段,求这mm段的和的最大值

    思路

    状态:dp[i][j]dp[i][j]表示在前jj个数中取出ii段的最大和

    状态转移方程:dp[i][j]=max(dp[i1][k],dp[i][j1])+num[j]  (i1kj1)dp[i][j]=max(dp[i-1][k],dp[i][j-1])+num[j] (i-1leq k leq j-1)

    由于mm范围未知,n106nleq 10^6,所以二维的dp方程无论是在时间上还是在空间上都是不允许的。

    那么我们就需要对这个方程进行优化:

    不难发现当前状态只与两个状态有关:

    1. jj个数和前j1j-1个数在一段里
    2. jj个数和前j1j-1个数不在一段里。

    根据这一点,我们把状态降成一维的数组,dp[j]dp[j]表示前jj个数分ii段时的最大和,然后用sum[j1]sum[j-1]来表示状态一的前j1j-1个数在前i1i-1段的最大和,dp[j1]dp[j-1]表示状态二的前j1j-1个数在前ii段的最大和。

    当前状态的转移方程为:dp[j]=max(dp[j1],sum[j1])+num[j]dp[j]=max(dp[j-1],sum[j-1])+num[j],持续更新dp与sum数组的值

    AC代码

    #include <stdio.h>
    #include <string.h>
    #include <iostream>
    #include <algorithm>
    #include <math.h>
    #include <limits.h>
    #include <map>
    #include <stack>
    #include <queue>
    #include <vector>
    #include <set>
    #include <string>
    #include <time.h>
    #define ll long long
    #define ull unsigned long long
    #define ms(a,b) memset(a,b,sizeof(a))
    #define pi acos(-1.0)
    #define INF 0x7f7f7f7f
    #define lson o<<1
    #define rson o<<1|1
    #define bug cout<<"-------------"<<endl
    #define debug(...) cerr<<"["<<#__VA_ARGS__":"<<(__VA_ARGS__)<<"]"<<"
    "
    const double E=exp(1);
    const int maxn=1e6+10;
    const int mod=1e9+7;
    using namespace std;
    int a[maxn];
    int dp[maxn];
    int sum[maxn];
    int main(int argc, char const *argv[])
    {
    	ios::sync_with_stdio(false);
    	cin.tie(0);
    	#ifndef ONLINE_JUDGE
    	    freopen("in.txt", "r", stdin);
    	    freopen("out.txt", "w", stdout);
    	    double _begin_time = clock();
    	#endif
    	int k,n;
    	while(cin>>k>>n)
    	{
    		int res;
    		for(int i=1;i<=n;i++)
    			cin>>a[i];
    		ms(dp,0);
    		ms(sum,0);
    		for(int i=1;i<=k;i++)
    		{
    			res=-INF;
    			for(int j=i;j<=n;j++)
    			{
    				dp[j]=max(sum[j-1],dp[j-1])+a[j];
    				sum[j-1]=res;
    				res=max(res,dp[j]);
    			}
    		}
    		cout<<res<<endl;
    	}
    	#ifndef ONLINE_JUDGE
    	    long _end_time = clock();
    	    printf("time = %lf ms.", _end_time - _begin_time);
    	#endif
    	return 0;
    }
    
  • 相关阅读:
    mysql 取年、月、日、时间
    第4步:创建RAC共享磁盘组
    第3步:添加用户与安装路径
    第2步:配置系统安装环境
    第1步:安装虚拟机+配置网络
    nodejs rar/zip加密压缩、解压缩
    使用shell脚本守护node进程
    抒发一下这些天用django做web项目的一些体会
    编写gulpfile.js文件:压缩合并css、js
    在NodeJS中使用流程控制工具Async
  • 原文地址:https://www.cnblogs.com/Friends-A/p/11054974.html
Copyright © 2011-2022 走看看