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 ...
随机推荐
- PHP数组合并的常见问题
一维数组的合并 <?php $arr1=array("a","b","c"); $arr2=array("c",& ...
- Visual Studio Code升级到0.5,提供对ES6的更好支持
(此文章同时发表在本人微信公众号"dotNET每日精华文章",欢迎右边二维码来关注.) 题记:题目即题记. 自从Visual Studio Code发布之后(最初是0.1),微软就 ...
- c文件操作 (转)
文件文件的基本概念 所谓“文件”是指一组相关数据的有序集合. 这个数据集有一个名称,叫做文件名. 实际上在前面的各章中我们已经多次使用了文件,例如源程序文件.目标文件.可执行文件.库文件 (头文件)等 ...
- 安装PIL
本来直接pip install PIL的,一直说找不到对应的版本,在廖雪峰老师的博客里发现原来PIL只更新到pytyon 2.7 后来就只有Pillow了,而我的版本是2.7.9 用的时候直接from ...
- 【项目经验】--EasyUI DataGrid之右键菜单
前两天验收项目,老总提了一个不是需求的需求,为什么这么说呢?因为我们的管理不到位!话说当天,我们UI系统下发了一个总文件,上面写着"各个系统找一个没有添加UI的模块去添加最新版本UI进行测试 ...
- java Clone使用方法详解
java"指针" Java语言的一个优点就是取消了指针的概念,但也导致了许多程序员在编程中常常忽略了对象与引用的区别,本文会试图澄清这一概念.并且由于Java不能 通过 ...
- Struts2零配置介绍(约定访问)
从struts2.1开始,struts2 引入了Convention插件来支持零配置,使用约定无需struts.xml或者Annotation配置 需要 如下四个JAR包 插件会自动搜索如下类 act ...
- filter应用案例一:分IP统计访问次数
统计工作需要在所有资源之前都执行,那么就可以放到Filter中了.用Map<String,Integer>装载统计的数据.Map创建时间(使用ServletContextListener, ...
- 【SQL Server】数据库是单个用户的 无法顺利进行操作 怎么解决
1.打开数据库 2.新建查询 ,输入以下的SQL 语句 DECLARE @SQL VARCHAR(MAX); SET @SQL='' SELECT @SQL=@SQL+'; KILL '+RTRIM( ...
- 动态添加PopupWindow
动态添加PopupWindow的方法private void showPopupWindow() { LayoutInflater inflater = LayoutInflater.from(thi ...