A. Max Sum Plus Plus

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 (子段1: 4;子段2:3 -2 3)

Sample Output

6
8

Hint

Huge input, scanf and dynamic programming is recommended.
 
题意:求最大M子段和
#include <iostream>
#include<cmath>
#include<cstring>
using namespace std;
const int MAX=;
const int INF=0x7fffffff;
int a[MAX];
int b[MAX];
int c[MAX];
int main()
{
int m,n;
while(cin>>n>>m)
{
for(int i=;i<=m;i++)
cin>>a[i];
memset(b,,sizeof(b));
memset(c,,sizeof(c));
int maxn;
for(int i=;i<=n;i++)
{
maxn=(-)*INF;
for(int j=i;j<=m;j++)
{
b[j]=max(b[j-]+a[j],c[j-]+a[j]);
c[j-]=maxn;
if(b[j]>maxn)
maxn=b[j];
}
}
cout<<maxn<<endl;
}
return ;
}

Max Sum Plus Plus——A的更多相关文章

  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. hdu 1003 Max sum(简单DP)

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

  8. HDU 1003 Max Sum

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

  9. 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. Linux系统安装及初始化(ubuntu14.04)

    Windows 7下硬盘安装Ubuntu 14.04图文教程 Ubuntu 官方已经发布了正式版的 Ubuntu 14.04 LTS,并宣称这是为云计算准备的版本.该版本在云平台和伸缩环境的可靠性.性 ...

  2. SQL简繁转换函数

    declare @jall nvarchar(4000),@fall nvarchar(4000) select @jall=N'啊阿埃挨哎唉哀皑癌蔼矮艾碍爱隘鞍氨安俺按暗岸胺案肮昂盎凹敖熬翱袄傲奥懊 ...

  3. java集群技术(转)

    来源:http://blog.csdn.net/cdh1213/article/details/21443239 序言 越来越多的关键应用运行在J2EE(Java2, Enterprise Editi ...

  4. MySQL中的SQL语言

    从功能上划分,SQL 语言可以分为DDL,DML和DCL三大类.1. DDL(Data Definition Language)数据定义语言,用于定义和管理 SQL 数据库中的所有对象的语言 :CRE ...

  5. mysql root用户 远程登录其它机器,看不到数据库

    在102上访问101上的数据库里,show databases;看不到里面的库, 需要在101上授权就可以了 GRANT ALL PRIVILEGES ON *.* TO 'root'@'192.16 ...

  6. 10324 Global Warming dfs + 二分

    时间限制:1000MS  内存限制:65535K提交次数:0 通过次数:0 题型: 编程题   语言: G++;GCC Description Global warming is a big prob ...

  7. [转]走向视网膜(Retina)的Web时代

    转载出处:http://www.w3cplus.com/css/towards-retina-web.html 维基百科将Retina译为“视网膜”."Retina"一词,原意是“ ...

  8. Dependency Properties

      Introduction Value resolution strategy The magic behind it How to create a DepdencyProperty Readon ...

  9. linux 安装程序

    tar文件打开 tar -xvf myfile.tar bz2文件打开

  10. 【BZOJ1864】[Zjoi2006]三色二叉树 树形DP

    1864: [Zjoi2006]三色二叉树 Description Input 仅有一行,不超过500000个字符,表示一个二叉树序列. Output 输出文件也只有一行,包含两个数,依次表示最多和最 ...