A - Max Sum Plus Plus

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

Submit Status

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(i m, 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.
         

/*
题意:给你一个长度为n的序列,让你求出最大m个字段的序列元素和 初步思路:动态规划最大m字段和,dp数组,dp[i][j]表示以a[j]结尾的,i个字段的最大和 两种情况:1.第a[j]元素单独作为第i个字段
2.第a[j]元素和前面的字段共同当做第i个字段 得到状态转移方程:dp[i][j]=max( dp[i][j-1]+a[j] , max(dp[i-1][t])+a[j]); 但是实际情况是,时间复杂度和空间复杂度都是相当的高,所以要进行时间和空间的优化:
将每次遍历的时候的max(dp[i-1][t]) 用一个数组d储存起来,这样就能省去寻找max(dp[i-1][t])的时间,
这样状态转移方程就变成了 dp[i][j]=max( dp[i][j-1]+a[j] , d[j-1]+a[j]), 会发现dp数组的可以
省去一维,因为每次都是和前一次的状态有关,所以可以记录前一次状态,再用一个变量tmp记录下dp[i][j-1],
这样方程就变成了 dp[i][j]=max( tmp+a[j] , d[j-1]+a[j]);这样就可以化简一下就是:dp[i][j]=
max( tmp , d[j-1])+a[j]; */
#include <bits/stdc++.h>
#define N 1000005
using namespace std;
int a[N];
int n,m;
int d[N];//用来存储j-1的位置用来存储 max(dp[i-1][t])
int main(){
// freopen("in.txt","r",stdin);
while(scanf("%d%d",&m,&n)!=EOF){
memset(d,,sizeof d);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
/*
dp[i][j]=max( dp[i][j-1]+a[j] , max(dp[i-1][t])+a[j])
*/
for(int i=;i<=m;i++){//遍历字段
int tmp = ;//用来记录dp[i-1][j]
for(int k = ; k <= i; ++k)
tmp += a[k];
//由于d[n]的位置是永远都用不到的,所以就用来存储最后的姐
d[n] = tmp;//前面的i项,每项都是一个段的时候 for(int j = i+; j <= n; ++j)
{
tmp = max(d[j-], tmp) + a[j]; //a[j]单独作为一个段的情况 和 前面的max(dp[i-1][t]) d[j-] = d[n];//将这个值保存下来 d[n] = max(d[n], tmp); //比较大小方便答案的输出
}
}
printf("%d\n",d[n]);
}
return ;
}

Max Sum Plus Plus的更多相关文章

  1. [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K

    Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...

  2. 2016huasacm暑假集训训练五 J - Max Sum

    题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/J 题意:求一段子的连续最大和,只要每个数都大于0 那么就会一直增加,所以只要和0 ...

  3. Max Sum

    Description Given a sequence a[1],a[2],a[3]......a[n], your job is to calculate the max sum of a sub ...

  4. HDU 1024 max sum plus

    A - Max Sum Plus Plus Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  5. hdu 1024 Max Sum Plus Plus

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

  6. hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行

    测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...

  7. Max Sum Plus Plus——A

    A. Max Sum Plus Plus Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To ...

  8. hdu 1003 Max sum(简单DP)

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

  9. HDU 1003 Max Sum

    Max Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  10. Leetcode: Max Sum of Rectangle No Larger Than K

    Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...

随机推荐

  1. 【个人笔记】《知了堂》MySQL中的数据类型

    MySQL中的数据类型 1.整型 MySQL数据类型 含义(有符号) tinyint(m) 1个字节  范围(-128~127) smallint(m) 2个字节  范围(-32768~32767) ...

  2. Java基础——集合源码解析 List List 接口

    今天我们来学习集合的第一大体系 List. List 是一个接口,定义了一组元素是有序的.可重复的集合. List 继承自 Collection,较之 Collection,List 还添加了以下操作 ...

  3. foreach循环中为什么不要进行remove/add操作

    先来看一段代码,摘自阿里巴巴的java开发手册 List<String> a = new ArrayList<String>(); a.add("1"); ...

  4. GitHub使用(二) - 新建文件夹

    1.首先打开我们已经建好的仓库 "test.github.com" 页面,可以看到如下图页面,找到“新建文件Create new file”按钮并点击.

  5. 快速搭建属于自己的数据库——mongodb

    为了真实模拟一个项目上线,拥有前端后端数据库都具备的功能,我选择了mongodb作为项目的数据库支持,这里分享一些mongodb的经验心得和血的教训. mongoddb安装 在本地安装 直接通过官网下 ...

  6. 面试题之-----String,StringBuffer,StringBuilder的区别

    String :字符串常量,值不能改变. String s="abc"; s=s+"def"; System.out.println(s); 输出结果为: ab ...

  7. hdu1251字典树递归算法

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Subm ...

  8. Angular学习笔记(一)

    本文为原创文章,转载请标明出处 目录 架构 模块 组件 模板 元数据 数据绑定 指令 服务 依赖注入 模板与数据绑定 1. 架构 模块 Angular 应用是模块化的,并且 Angular 有自己的模 ...

  9. HTTPS与MITM

    HTTPS:基于SSL/TSL的HTTP协议 MITM:Man-In-The-Middle中间人攻击 Https下中间人攻击的思路: 1 去https化 2 向CA申请相似域名的证书 防范: 睁大双眼

  10. install xdebug

    安装准备 安排php的xdebug扩展,在php.ini上配置xdebug.通过phpinfo或者php-m 查看 [Xdebug] zend_extension ="D:\upupw7\P ...