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. Html语言,使用<a>标签发送电子邮件

    <a>标签有一个功能是可以链接Email地址,使用mailto能让访问者便捷向网站管理者发送电子邮件. 使用mailto 属性时请参考下表: 如果mailto里同时有多个参数,第一个参数必 ...

  2. Hive任务优化(1)

    一个Hive查询生成多个Map Reduce Job,一个Map Reduce Job又有Map,Reduce,Spill,Shuffle,Sort等多个阶段,所以针对Hive查询的优化可以大致分为针 ...

  3. Springmvc学习笔记(一)

    一.sprinvmvc的介绍 1.1.Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 Web 应用程序的全 ...

  4. 在Ubuntu上安装arm-linux-gcc的问题

    由于之前将Ubuntu的更新关掉了,所以导致我下载32位兼容包一直出错. 在arm-linux-gcc 安装之后,还不能编译程序的话,首先看自己的系统是多少位的,因为网上大部分的安装包都是32位的,所 ...

  5. hdu 4123--Bob’s Race(树形DP+RMQ)

    题目链接 Problem Description Bob wants to hold a race to encourage people to do sports. He has got troub ...

  6. .Net 内存对象分析

    在生产环境中,通过运行日志我们会发现一些异常问题,此时,我们不能直接拿VS远程到服务器上调试,同时日志输出的信息无法百分百反映内存中对象的状态,比如说我们想查看进程中所有的Socket连接状态.服务路 ...

  7. ZOJ3508 The War 贪心,最大流

    传送门:I Am Here 常规解法是贪心,但是在复习最大流的写法,因此用sap来写的.思路是很好想的 #include<cstdio> #include<cstdlib> # ...

  8. 解决Android5.0以下Dialog引起的内存泄漏

    最近项目开发中,开发人员和测试人员均反应在android5.0以下手机上LeakCanary频繁监控到内存泄漏,如下图所示,但凡用到Dialog或DialogFragment地方均出现了内存泄漏. 如 ...

  9. MYSQL数据库45道练习题

    --第一题查询Student表中的所有记录的Sname.Ssex和Class列.select Sname,Ssex,Class from Student;--第二题查询教师所有的单位即不重复的Depa ...

  10. Appium python自动化测试系列之移动自动化测试前提(一)

    1.1 移动自动化测试现状 因为软件行业越来越发达,用户的接受度也在不断提高,所以对软件质量的要求也随之提高,当然这个也要分行业,但这个还是包含了大部分.因为成本.质量的变化现在对自动化测试的重视度越 ...