zoukankan      html  css  js  c++  java
  • HDU 1024 Max Sum Plus Plus 动态规划

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

    题目大意:n个数分成两两不相交的m段,求使这m段和的最大值。

    解题思路:比较坑的点:n能过;long long超时,int AC。 

    dp[i][j]:= 在选择第i个数的情况下前i个数分成j段的最大值
    dp[i][j] = max(dp[i - 1][j] + a[i], max(dp[x][j - 1] -> dp[x][j - 1]) + a[i]) x < i

    由于n<1000000,并且更新dp[i][j]时只用到了j和j-1的部分,因此采用滚动数组记录选择第i个人时前i个人分成j段的和最大值;同时,max(dp[x][j - 1] -> dp[x][j - 1]) 可以在更新dp[i]的同时记录,这样就将时间复杂度降低到了n2 (所以为什么n2能过???)

    另外就是一些细节问题。

    代码:

     1 const int inf = 0x3f3f3f3f;
     2 //dp[i][j]:= 在选择第i个数的情况下前i个数分成j段的最大值
     3 //dp[i][j] = max(dp[i - 1][j] + a[i], max(dp[x][j - 1] -> dp[x][j - 1]) + a[i]) x < i
     4 const int maxn = 1e6 + 5;
     5 int dp[maxn];
     6 int pre[maxn];
     7 int a[maxn], n, m;
     8 
     9 int solve(){
    10     memset(dp, 0, sizeof(dp));
    11     memset(pre, 0, sizeof(pre));
    12     int tmax = -inf;
    13     for(int j = 1; j <= m; j++){
    14         tmax = -inf;//记录前i个人本次的最大值 
    15         for(int i = j; i <= n; i++){
    16             dp[i] = max(dp[i - 1] + a[i], pre[i - 1] + a[i]);//使用的是未更新的值,对应j-1的 
    17             pre[i - 1] = tmax;    //再更新 
    18             tmax = max(tmax, dp[i]);
    19         }
    20     }
    21     return tmax; //不一定是dp[n],最后一个可能不用选 
    22 }
    23 
    24 int main(){
    25     while(scanf("%d %d", &m, &n) != EOF){
    26         for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    27         int ans = solve();
    28         printf("%d
    ", ans);
    29     }
    30 }

    题目:

    Max Sum Plus Plus

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


    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(极光炫影)
  • 相关阅读:
    NIO 学习笔记
    Spring Boot 学习笔记
    Java集合框架
    StringBuffer&StringBuilder类
    String 类
    Java 重写 hashCode() 和 equals() 方法
    Java 基本数据类型 && 位运算
    [SequenceFile_1] Hadoop 序列文件
    Windows 下端口被占用
    Java 反射机制
  • 原文地址:https://www.cnblogs.com/bolderic/p/7350991.html
Copyright © 2011-2022 走看看