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

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

    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
     
    题目大意 在长度为n的序列中找m个不相交的连续子序列使其和最大,输出最大和
    状态转移 dp[i][j] = max(dp[i - 1][k] + num[j] ),( 1 <= k < j ),ans = max( dp[i][k] ),( 1 <= k <= n ),dp[i][j]表示取num[j]的情况下前j个数字i分的最大值;但是复杂度为n^3,考虑到求dp[i][j]时所用的dp[i][k]即为前j-1个数i分时的ans,可以用两个数组分别存储,将复杂度优化为n^2。
     
     1 #include<iostream>
     2 #include<cstring>
     3 #include<algorithm>
     4 using namespace std;
     5 
     6 int num[1000005];
     7 int dp[1000005];
     8 int mmax[1000005];
     9 int ans;
    10 int m, n;
    11 
    12 int main(){
    13     ios::sync_with_stdio( false );
    14 
    15     while( cin >> m >> n ){
    16         memset( dp, 0, sizeof( dp ) );
    17         memset( mmax, 0, sizeof( mmax ) );
    18 
    19         for( int i = 1; i <= n; i++ )
    20             cin >> num[i];
    21 
    22         for( int i = 1; i <= m; i++ ){
    23             ans = -0x3f3f3f3f;
    24             for( int j = i; j <= n; j++ ){
    25                 dp[j] = max( dp[j - 1] + num[j], mmax[j - 1] + num[j] );
    26                 mmax[j - 1] = ans;
    27                 ans = max( ans, dp[j] );
    28             }
    29         }
    30 
    31         cout << ans << endl;
    32     }
    33 
    34     return 0;
    35 }
     
     
  • 相关阅读:
    自制flash3D变换类
    Alchemy的使用和多项式批量计算的优化
    Bresenham直线扫描算法
    模拟流体粒子运动
    任意多边形的碰撞检测——向量积判断方法
    漂亮的雪花飘落和堆积效果
    发个简单怡情的粒子随机运动
    三次贝塞尔曲线绘制算法(优化过)
    失败是成功之母
    typeid操作符
  • 原文地址:https://www.cnblogs.com/hollowstory/p/5356555.html
Copyright © 2011-2022 走看看