zoukankan      html  css  js  c++  java
  • K

    K - Max Sum Plus Plus

    Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

     

    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 S 1, S 2, S 3, S 4 ... S x, ... S n (1 ≤ x ≤ n ≤ 1,000,000, -32768 ≤ S x ≤ 32767). We define a function sum(i, j) = S i + ... + S j (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(i 1, j 1) + sum(i 2, j 2) + sum(i 3, j 3) + ... + sum(im, j m) maximal (i x ≤ i y ≤ j x or i x ≤ j y ≤ j x 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(i x, j x)(1 ≤ x ≤ m) instead. ^_^ 

    Input

    Each test case will begin with two integers m and n, followed by n integers S 1, S 2, S 3 ... S n
    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 ,n (n<=1000000) 两个整数,然后第二行 n 个数,求 m 段不相交连续序列最大和。

    这篇博客写得十分详细

    http://www.cnblogs.com/kuangbin/archive/2011/08/04/2127085.html

    dp[i][j]代表的状态是 i 段连续序列的最大和,并且最后一段一定包含 num[j]

    所以写出状态转移方程 dp[i][j]=max{dp[i][j-1]+a[j],max{dp[i-1][t]}+a[j]} 0<t<j-1 

    dp[i][j-1]代表接上上一个元素,后面代表自己独立成一组

    n很大,只能用滚动数组

    不断更新状态,用一个数据 big 保存 i - 1 段最大的和,最后直接输出,就是答案

    436ms

     1 #include <stdio.h>
     2 #include <string.h>
     3 
     4 #define inf 0x7ffffff
     5 #define MAXN 1000005
     6 int num[MAXN];
     7 int dp[MAXN];
     8 int pre[MAXN];
     9 
    10 int max(int a,int b)
    11 {
    12     return a>b? a:b;
    13 }
    14 
    15 int main()
    16 {
    17     int m,n;
    18     int i,j,big;
    19     while (scanf("%d%d",&m,&n)!=EOF)
    20     {
    21         for (i=1;i<=n;i++)
    22         {
    23             scanf("%d",&num[i]);
    24             pre[i]=0;
    25         }
    26 
    27         pre[0]=0;
    28         dp[0]=0;
    29 
    30         for (i=1;i<=m;i++)
    31         {
    32             big=-inf;
    33             for (j=i;j<=n;j++)
    34             {
    35                 dp[j]=max(dp[j-1],pre[j-1])+num[j];//连续的最大和,或者不连续的最大和
    36                 pre[j-1]=big;      //保存 i - 1 段 最大和
    37                 big=max(big,dp[j]);//保证是 i 段最大的和
    38             }
    39         }
    40         printf("%d
    ",big);
    41     }
    42     return 0;
    43 }
    View Code



  • 相关阅读:
    codova 打包vue项目的坑
    vscode 开发wtl 笔记
    redis
    展开/收缩 ul
    ueditor
    xml
    NPOI
    滚动效果,有些浏览器不支持
    fileupload控件上传、文件下载
    excel函数
  • 原文地址:https://www.cnblogs.com/haoabcd2010/p/5760315.html
Copyright © 2011-2022 走看看