地址: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

Hint

Huge input, scanf and dynamic programming is recommended.

 
 
一开始,题目都没看懂,(os:题目讲的什么鬼,英语差就是心酸= =)
后来看懂了题目意思后也没做出来,没想到那种dp方法,好心累,估计是dp题做得少的原因吧。。
之后看了看了几个人的博客,才会的,原地址http://blog.csdn.net/lishuhuakai/article/details/8067474
还有一个的忘了。。。
先讲下题目意思吧:先讲下连续最大子段和,就是给你n个数,a[1]....1[n],求a[i]+.a[i+1]...+a[j]的最大值。
    此时状态转移方程:dp[i]=a[i]+(dp[i-1]>0?dp[i-1]:0)(dp【i】代表选了a【i】时的最大子段和)
       呃,不知道怎么表达了,上图吧:
    

dp[i]\a[i] 2 -1 3 -8 9
1 2        
2   1      
3     4    
4       -4  
5         9
    对于连续最大和,只需要扫描一遍数组就好了
    1024题呢,就是把n个数分成x(x∈[m,n])段后,求这其中m个的子段和(不相交)的最大值;  
    由连续最大子段和的解法可以推广到求m个最大字段和,
             dp[i][j] = max(dp[i][j-1],max(dp[i-1][k]))+a[j];(i-1<=k<=j-1)
    其中dp【i】【j】时,前j个元素中取i个子段的最大和(一定取了a【j】)
    所以由dp[i][j-1]到dp[i][j] 时,有两种取法:
    1:a[j]和前一个以a[j-1]的子段合并
    2:独立成一个子段;
      以题目的第二组数据为例:
    

dp i\j) -1 4 -2 3 -2 3
1 -1 4 2 5 3 6
2 \ 3 2 7 5 8
3 \ \ 1 6 5 10
4 \ \ \ 4 4 9
5 \ \ \ \ 2 7
    
 
 
 
 
maxtemp \ \ \ \ \ \
1 -1 4 4 5 5 \
2 \ 3 3 7 7 \
3 \ \ 1 6 6 \
4 \ \ \ 4 9 \
5 \ \ \ \ 2 \
  \ \ \   \ \
 
 
 
 
 
   
没写的代表不用算。。。
    这样dp方程就有了,不过因为题目所给的数据较大,1 ≤ n ≤ 1,000,000,
    空间复杂度:m*n,很容易就超了
    时间复杂度:n^3,也是会超的
    所以继续优化,容易看出,每次使用dp方程时,其实只涉及到两个数组dp[i][k]和dp[i-1][k],所以可以化简为两个数组dp1[i],dp2[i]储存。
    这样空间复杂度就降低到m了
    然而时间复杂度没变,继续优化,我们可以用另一个数组maxtemp[i]来储存max(dp[i-1][k])(i-1<=k<=j-1),
    所以dp方程变为:
        dp[i][j] = max(dp[i][j-1],maxtemp[i-1])+a[j];
    这样时间复杂度就变成n^2了,可以接受了。
    其实,空间还可以优化,用一个dp数组就可以了,因为dp[i]从左向右递推时,只和dp[i-1],maxtemp[i-1]有关,
    所以可以删去dp2[i].
    上代码:
 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <vector> #define N 1000010
#define PI acos((double)-1)
#define E exp(double(1))
using namespace std;
int dp1[N];
int maxtemp[N];
int a[N]; int main(void)
{
int n, m, max1;
while (scanf("%d%d", &m, &n) == )
{
for (int i = ; i <= n; i++)
scanf("%d", &a[i]);
dp1[] = maxtemp[] = max1 = a[];
for (int i = ; i <= n; i++)
{
if (max1 >= )
{
dp1[i] = a[i] + max1;
max1 += a[i];
}
else
{
dp1[i] = a[i];
max1 = a[i];
}
}
for (int i = ; i <= m; i++)
{
maxtemp[i-] = dp1[i-];
for (int j = i ; j < n; j++)
{
if (maxtemp[j - ]<dp1[j])
{
maxtemp[j] = dp1[j];
}
else
maxtemp[j] = maxtemp[j - ];
}
for (int j = i; j <= n; j++)
{
if (i == j)
dp1[j] = maxtemp[j - ] + a[j];
else
dp1[j] = max(dp1[j - ], maxtemp[j - ]) + a[j];
}
}
max1 = dp1[m];
for (int i = m; i <= n; i++)
if (max1<dp1[i])
max1 = dp1[i];
cout << max1 << endl; }
return ;
}

杭电1024Max Sum Plus Plus的更多相关文章

  1. 杭电1024----Max Sum Plus Plus

    /* 这题还没有理解透彻.某个dalao也不写注释.只能自己理解了... 先求为i个元素(1<=i<=M)为一个区间的最大和,保证元素个数大于等于i个,递推到M个即可 借鉴原址:http: ...

  2. 杭电1003-Max Sum

    Max Sum Problem Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the ...

  3. 杭电1003_Max Sum

    这是原题的链接http://acm.hdu.edu.cn/showproblem.php?pid=1003 起初我是利用暴力的方法,求出所有序列的和的情况,每取一个序列就和以知道的最大和作对比,取大者 ...

  4. 杭电ACM2058--The sum problem

    http://acm.hdu.edu.cn/showproblem.php?pid=2058 以为简单的穷举就完了,结果是一直Time Limit Exceeded.. 这是代码: #include ...

  5. acm入门 杭电1001题 有关溢出的考虑

    最近在尝试做acm试题,刚刚是1001题就把我困住了,这是题目: Problem Description In this problem, your task is to calculate SUM( ...

  6. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

  7. 高手看了,感觉惨不忍睹——关于“【ACM】杭电ACM题一直WA求高手看看代码”

    按 被中科大软件学院二年级研究生 HCOONa 骂为“误人子弟”之后(见:<中科大的那位,敢更不要脸点么?> ),继续“误人子弟”. 问题: 题目:(感谢 王爱学志 网友对题目给出的翻译) ...

  8. 杭电ACM(1002) -- A + B Problem II 大数相加 -提交通过

    杭电ACM(1002)大数相加 A + B Problem II Problem DescriptionI have a very simple problem for you. Given two ...

  9. Help Johnny-(类似杭电acm3568题)

    Help Johnny(类似杭电3568题) Description Poor Johnny is so busy this term. His tutor threw lots of hard pr ...

随机推荐

  1. pycharm重置设置,恢复默认设置

    备忘,备忘,备忘 window 系统 找到下方目录-->删除. 再重新打开pycharm # Windows Vista, 7, 8, 10: <SYSTEM DRIVE>\User ...

  2. 05 Java图形化界面设计——布局管理器之GridLayout(网格布局)

    网格布局特点: l  使容器中的各组件呈M行×N列的网格状分布. l  网格每列宽度相同,等于容器的宽度除以网格的列数. l  网格每行高度相同,等于容器的高度除以网格的行数. l  各组件的排列方式 ...

  3. HDU-1095-A+B for Input-Output Practice (VII)(多一个空格?)

    A+B for Input-Output Practice (VII) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32 ...

  4. Dependency Property 依赖属性

    依赖属性就是一种可以自己没有值,并能通过使用Binding从数据源获得值(依赖在别人身上)的属性.拥有依赖属性的对象称为“依赖对象”. WPF开发中,必须使用依赖对象作为依赖属性的宿主,使二者结合起来 ...

  5. ChemDraw绘制DNA结构的技巧

    对生物有一定了解的朋友都知道DNA是染色体的重要组成部分,DNA结构中包含重要的遗传物质,孩子的DNA来自父母DNA的组合,这就是为什么“一家人相像”的奥秘所在.ChemDraw虽然号称是化学结构绘制 ...

  6. poj 3653(最短路)

    题目链接:http://poj.org/problem?id=3653 思路:题目意思很简单,就是二维平面上的图,要求起点到终点的最短路.建图略坑,需要坐标映射,化二维为一维.然后就是Dijkstra ...

  7. C#实现动态编译代码

    /*------------------------------------------------------------------------------ * Copyright (C) 201 ...

  8. sql查询用nolock

    大家在写查询时,为了性能,往往会在表后面加一个nolock,或者是with(nolock),其目的就是查询是不锁定表,从而达到提高查询速度的目的. 什么是并发访问:同一时间有多个用户访问同一资源,并发 ...

  9. poj2046

    Gap Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 1829   Accepted: 829 Description Le ...

  10. shop++源码反编译----随笔

    一.applicationContext-mvc.xml配置 1.读取配置文件 <context:property-placeholder location="classpath*:/ ...