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

    http://acm.hdu.edu.cn/showproblem.php?pid=1024

    Max Sum Plus Plus

    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 ≤ jxor 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.
     
    题意:将一串数字分成m段子序列,求最大可能达到的总和。
     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<iostream>
     5 using namespace std;
     6 #define N 1000005
     7 #define INF -1000000000
     8 int num[N],dp[N],mmax[N];
     9 /*
    10 状态DP[i][j]
    11 前j个数可以分成i组的和的最大值
    12 DP[i][j]=max(dp[i][j-1]+num[j], max( dp[i-1][k] )+num[j]),0<k<j;
    13 dp[i][j-1]是把num[j]加入到前面的一个组,
    14 max(dp[i-1][k])+num[j]是把num[j]重新分在另一个组里面,
    15 而max(dp[i-1][k])是分成i-1个组时候的和的最大值
    16 开一个mmax数组每次都可以保存分成i-1个组的时候使用前j-1个数时候最大值
    17 时间复杂度O(mn)
    18 */
    19 int main()
    20 {
    21     int n,m;
    22     while(~scanf("%d%d",&m,&n)){
    23         for(int i=1;i<=n;i++){
    24             scanf("%d",num+i);
    25         }
    26         memset(dp,0,sizeof(dp));
    27         memset(mmax,0,sizeof(mmax));
    28         int ans;
    29         for(int i=1;i<=m;i++){
    30             ans=INF;
    31             for(int j=i;j<=n;j++){
    32                 dp[j]=max(dp[j-1],mmax[j-1])+num[j];
    33                 mmax[j-1]=ans;
    34                 ans=max(dp[j],ans);
    35             }
    36         }
    37         cout<<ans<<endl;
    38     }
    39     return 0;
    40 }

    2016-06-21

  • 相关阅读:
    #JavaScript 闭包问题详解 #打倒心魔
    Typora + cnblog 图片自动上传 (超详细哦)
    #FUNCTION#CALL对象中的函数内作用域问题.md
    #windows #Github #HOST
    #######对象迭代器######
    #为什么不建议使用for...in 去遍历数组
    #前后端附件传输,去重的一种方式#解决方案
    #页面滚动刷新的实现原理 #下拉刷新#上拉刷新#drag to fresh
    自己动手实现一个阻塞队列
    APC注入
  • 原文地址:https://www.cnblogs.com/fightfordream/p/5602302.html
Copyright © 2011-2022 走看看