Max Sum Plus Plus

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

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(极光炫影)
 
 题意:
   给定一段连续数字串,要你求出m个连续子串的最大值。
 比如
   下面这个样列
    2  6 -1 4 -2 3 -2 3
    给出一个长度为6的连续数字串,要你求出这个串中连续的m=2个子串的子串之和.......

对样列的一个数值分析
aa -1 4 -2 3 -2 3
第一遍 maxc -1 4 4 4 5 \
dp -1 4 2 5 3 6
第二遍 maxc -1 3 3 7 7 \
dp -1 3 2 7 5 8
             
             

代码

 #include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
int a[1000001],dp[1000001],max1[1000001];
int max(int x,int y){
return x>y?x:y;
}
int main(){
int i,j,n,m,temp;
while(scanf("%d%d",&m,&n)!=EOF)
{
dp[0]=0;
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
dp[i]=0;
max1[i]=0;
}
max1[0]=0;
for(i=1;i<=m;i++){
temp=-0x3f3f3f3f;
for(j=i;j<=n;j++){
dp[j]=max(dp[j-1]+a[j],max1[j-1]+a[j]);
max1[j-1]=temp;
temp=max(temp,dp[j]);
}
}
printf("%d\n",temp);
}
return 0;
}

优化后的代码:

 /*hdu 1024 @coder Gxjun*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
const int maxn=;
int aa[maxn],dp[maxn],maxc[maxn];
int max(int a,int b){
return a>b?a:b;
}
int main()
{
int n,m,i,j,temp;
while(scanf("%d%d",&m,&n)!=EOF){
memset(maxc,,sizeof(int)*(n+));
memset(dp,,sizeof(int)*(n+));
for(i=;i<=n;i++)
scanf("%d",&aa[i]);
for(i=;i<=m;i++){
temp=-0x3f3f3f3f;
for(j=i;j<=n;j++){
dp[j]=max(dp[j-],maxc[j-])+aa[j];
maxc[j-]=temp;
temp=max(temp,dp[j]);
}
}
printf("%d\n",temp);
}
}

hdu---1024Max Sum Plus Plus(动态规划)的更多相关文章

  1. HDU 1024Max Sum Plus Plus(最大m字段和)

    /* 动态转移方程:dp[i][j]=max(dp[i-1]+a[i], max(dp[t][j-1])+a[i]) (j-1<=t<i) 表示的是前i个数j个字段和的最大值是多少! */ ...

  2. HDU 1176 免费馅饼 (动态规划)

    HDU 1176 免费馅饼 (动态规划) Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小径上,忽然天上掉下大把大把的馅饼.说来gameboy的人品实在是太好了,这馅饼 ...

  3. HDU 1074 Doing Homework (动态规划,位运算)

    HDU 1074 Doing Homework (动态规划,位运算) Description Ignatius has just come back school from the 30th ACM/ ...

  4. HDOJ(HDU).1258 Sum It Up (DFS)

    HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...

  5. HDU 1024 Max Sum Plus Plus [动态规划+m子段和的最大值]

    Max Sum Plus Plus Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tot ...

  6. hdu 1024 Max Sum Plus Plus (动态规划)

    Max Sum Plus PlusTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  7. 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 b ...

  8. hdu 1258 Sum It Up(dfs+去重)

    题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = ...

  9. 数论 --- 费马小定理 + 快速幂 HDU 4704 Sum

    Sum Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=4704 Mean: 给定一个大整数N,求1到N中每个数的因式分解个数的 ...

  10. HDU 1231 最大连续子序列 &&HDU 1003Max Sum (区间dp问题)

    C - 最大连续子序列 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit ...

随机推荐

  1. VIM如何将全部内容复制并粘贴到外部

    ubuntu默认安装的vim是不支持系统剪切.粘贴版的,需要执行以下安装:sudo apt-get install vim-gnome 注意要让vim支持系统粘贴板,首先执行sudo apt-get ...

  2. 最大熵模型(Maximum Etropy)—— 熵,条件熵,联合熵,相对熵,互信息及其关系,最大熵模型。。

    引入1:随机变量函数的分布 给定X的概率密度函数为fX(x), 若Y = aX, a是某正实数,求Y得概率密度函数fY(y). 解:令X的累积概率为FX(x), Y的累积概率为FY(y). 则 FY( ...

  3. CUBRID学习笔记 8 复制数据库

    1  export  database  类似sqlserver的分离数据库 cubrid unloaddb demodb分离后生成三个文件 demodb_objects, demodb_indexe ...

  4. 使用Invoke、委托函数

    //Invoke(new MethodInvoker(delegate() //{ // DataBind(); //}));

  5. CSS笔记(十一)CSS3之边框

    参考:http://www.w3school.com.cn/css3/css3_border.asp 圆角边框 <!DOCTYPE html> <html> <head& ...

  6. underscore的封装和扩展

    // 1. 不污染全局环境 (function() { // 2. 保留之前同名变量 var previousUnderscore = window._; var _ = function(obj) ...

  7. Android中序列化对象到XMl 和 XML反序列化为对象

    package com.example.xmloperation; import java.io.File; import java.io.FileOutputStream; import java. ...

  8. 妙用||和&&

    &&和||在JQuery源代码内尤为使用广泛,由于本人没有系统的学习js,所以只能粗略的自我理解出来,希望大家指点下.粗略理解如下: a() && b() :如果执行a ...

  9. 用Maven创建第一个项目

    1.在Eclipse左侧的空白处点击鼠标右键,选择:New>Other : 2.选择Maven项目,点击"Next"按钮: 3.保持默认,直接点击“Next”按钮: 4.选择 ...

  10. ASP.NET MVC HtmlHelper用法集锦

    ASP.NET MVC HtmlHelper用法集锦 在写一个编辑数据的页面时,我们通常会写如下代码 1:<inputtype="text"value='<%=View ...