Max Sum Plus Plus——A
A. Max Sum Plus Plus
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
Process to the end of file.
Output
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
#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的更多相关文章
- [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 ...
- 2016huasacm暑假集训训练五 J - Max Sum
题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/J 题意:求一段子的连续最大和,只要每个数都大于0 那么就会一直增加,所以只要和0 ...
- 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 ...
- HDU 1024 max sum plus
A - Max Sum Plus Plus Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I6 ...
- hdu 1024 Max Sum Plus Plus
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu 1003 MAX SUM 简单的dp,测试样例之间输出空行
测试样例之间输出空行,if(t>0) cout<<endl; 这样出最后一组测试样例之外,其它么每组测试样例之后都会输出一个空行. dp[i]表示以a[i]结尾的最大值,则:dp[i ...
- hdu 1003 Max sum(简单DP)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Problem ...
- HDU 1003 Max Sum
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- 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 ...
随机推荐
- loj 1377 (bfs)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1377 思路:这道题只要处理好遇到"*"这种情况就可以搞定了.我们可 ...
- 智能车学习(七)——按键矩阵的实现
一.原理说明 就是按键矩阵代码书写的一个说明,就是讲K5到K7先输出高电平,而K1和K4则调成上拉输入,如果检测到K1到K4有一个变为0,说明有按键按下去,立刻进行转换,是的K1到K4设置为输出高电平 ...
- zookeeper源码分析三LEADER与FOLLOWER同步数据流程
根据二)中的分析,如果一台zookeeper服务器成为集群中的leader,那么一定是当前所有服务器中保存数据最多的服务器,所以在这台服务器成为leader之后,首先要做的事情就是与集群中的其它服务器 ...
- LoadRunner 场景运行error的几种情况
一. Error -27727: Step download timeout (120 seconds)has expired when downloading resource(s). Set th ...
- PHPExcel设置数据格式的几种方法
转自:http://www.cnblogs.com/guangxiaoluo/archive/2013/11/19/3431846.html 解决 PHPExcel 长数字串显示为科学计数 在exce ...
- JMeter正则表达式-学习(3)
同时关联多个值的方法: { : ", : "results": : [ : : { : : : "total_earnings":"&quo ...
- HTTP协议开发应用-HTTP&XML协议栈开发
Netty HTTP+XML协议栈开发 由于HTTP协议的通用性,很多异构系统间的通信交互采用HTTP协议,通过HTTP协议承载业务数据进行消息交互,例如非常流行的HTTP+XML或者RESTful+ ...
- Spring事务解析3-增强方法的获取
从InfrastructureAdvisorAutoProxyCreator的层次结构中可以看到,InfrastructureAdvisorAutoProxyCreator间接实现了SmartInst ...
- kafka基本原理学习
下载安装地址:http://kafka.apache.org/downloads.html 原文链接:http://www.jasongj.com/2015/01/02/Kafka深度解析 Kafk ...
- C++11 std::chrono库详解
所谓的详解只不过是参考www.cplusplus.com的说明整理了一下,因为没发现别人有详细讲解. chrono是一个time library, 源于boost,现在已经是C++标准.话说今年似乎又 ...