• HDU1024(最大M子段和)


    Max Sum Plus Plus

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

    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
     
    Hint
    Huge input, scanf and dynamic programming is recommended.
     
    Author
    JGShining(极光炫影)
     
    dp[i][j]表示前j个数组成的序列最大i子段和,则状态转移方程为
    dp[i][j]=max{dp[i][j-1]+a[j],max{dp[i-1][t]}+a[j]} i-1=<t<j-1
    优化1:本题数据范围太大, 所以可以用滚动数组优化
    优化2:用last数组记录上一层循环得到的最大值,具体见代码
    /*
    ID: LinKArftc
    PROG: 1024.cpp
    LANG: C++
    */
    
    #include <map>
    #include <set>
    #include <cmath>
    #include <stack>
    #include <queue>
    #include <vector>
    #include <cstdio>
    #include <string>
    #include <climits>
    #include <utility>
    #include <cstdlib>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    #define eps 1e-8
    #define randin srand((unsigned int)time(NULL))
    #define input freopen("input.txt","r",stdin)
    #define debug(s) cout << "s = " << s << endl;
    #define outstars cout << "*************" << endl;
    const double PI = acos(-1.0);
    const double e = exp(1.0);
    const int inf = 0x3f3f3f3f;
    const int INF = 0x7fffffff;
    typedef long long ll;
    
    const int maxn = 1000010;
    
    int num[maxn], dp[maxn], last[maxn];
    
    int main() {
        int n, m;
        while (~scanf("%d %d", &m, &n)) {
            memset(dp, 0, sizeof(dp));
            memset(last, 0, sizeof(last));
            for (int i = 1; i <= n; i ++) scanf("%d", &num[i]);
            int ma;
            for (int i = 1; i <= m; i ++) {//分成i段
                ma = INT_MIN;
                for (int j = i; j <= n; j ++) {//前j个数
                    dp[j] = max(dp[j-1], last[j-1]) + num[j];//dp[j]表示前j个划分成i段最大值,last[j-1]表示前j-1个划分成i-1段最大值
                    last[j-1] = ma;
                    ma = max(ma, dp[j]);
                }
                last[n] = ma;
            }
            printf("%d
    ", ma);
        }
    
        return 0;
    }
  • 相关阅读:
    vs c++配置opencv(1)
    vs2013搭建团队版本控制 TFS、SVN
    robomongo
    Node log4js
    node.async.auto
    p2.js物理引擎学习
    pomelo
    Node.mongoose
    Node.Buffer
    Node安装及搭建简单HTTP服务器
  • 原文地址:https://www.cnblogs.com/LinKArftc/p/4963191.html
Copyright © 2020-2023  润新知