• HDU 1024 Max Sum Plus Plus (动态规划 最大M字段和)


     

    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段互不相交的子段,求最大的子段和

     思路:在这里向大家推荐一篇大佬的博客,讲解的清晰明了,让我获益匪浅 https://blog.csdn.net/winter2121/article/details/72848482  Orz

      大佬的博客写的很清晰,而且附上图解很容易弄懂

      dp[i][j] 代表前i个数并且以第i个数为末尾划分成了j段的最大和。

      而dp[i][j] 有两种情况:1. 将第i个数划分到前面这一段取

                2.将第i个数单独拿出来作为一段的开始

      dp[i][j] = max(dp[i-1][j],max(前i个数中的最大和))+a[j];

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 
     5 using namespace std;
     6 typedef long long LL;
     7 const int INF = 0x3f3f3f3f;
     8 const int maxn = 1000005;
     9 LL n, m;
    10 LL dp[maxn][2], a[maxn];//dp[i][j]代表以第i个数为结尾且分成j段的最大和
    11 int main()
    12 {
    13     ios::sync_with_stdio(false);
    14     while (/*cin>>m>>n*/scanf("%lld%lld", &m, &n) != EOF) {
    15         for (int i = 1; i <= n; i++)scanf("%lld", &a[i]);
    16             /*cin >> a[i];*/
    17         for (int i = 0; i <= n; i++)dp[i][0] = dp[i][1] = 0;
    18         for (int i = 1, k = 1; i <= m; i++, k ^= 1) {
    19             LL maxpre = -INF; dp[i - 1][k] = -INF;//避免与后面for循环的影响
    20             for (int j = i; j <= n - m + i; j++) {
    21                 maxpre = max(maxpre, dp[j-1][k ^ 1]);//寻找上一行第i个数之前的最大值
    22                 dp[j][k] = max(dp[j - 1][k], maxpre) + a[j];
    23             }
    24         }
    25         LL ans = -INF;
    26         for (int i = m; i <= n; i++)
    27             ans = max(ans, dp[i][m&1]);
    28         //cout << ans << endl;
    29         printf("%lld
    ", ans);
    30     }
    31     return 0;
    32 }
    View Code
     
  • 相关阅读:
    【HDU1724】Ellipse-自适应Simpson积分法
    【HDU1724】Ellipse-自适应Simpson积分法
    【51Nod1227】平均最小公倍数-杜教筛
    【51Nod1227】平均最小公倍数-杜教筛
    【HDU5628】Clarke and math-狄利克雷卷积+快速幂
    【HDU5628】Clarke and math-狄利克雷卷积+快速幂
    deleted
    deleted
    deleted
    deleted
  • 原文地址:https://www.cnblogs.com/wangrunhu/p/9519273.html
Copyright © 2020-2023  润新知