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 ...
随机推荐
- WPF之MVVM(Step1)——自己实现ICommand接口
开发WPF应用程序,就不得不提MVVM.下面偶将展示MVVM中简单的实现,其中主要在于ICommand的实现上,不过这种实现方式,应该不会有多少人在开发中使用,在此仅作学习使用. 准备: 界面绘制,简 ...
- Kmeans聚类算法原理与实现
Kmeans聚类算法 1 Kmeans聚类算法的基本原理 K-means算法是最为经典的基于划分的聚类方法,是十大经典数据挖掘算法之一.K-means算法的基本思想是:以空间中k个点为中心进行聚类,对 ...
- Accelerating Matlab
Matlab is a very useful programming environment, but it also has many inefficiencies. You might thin ...
- try : finally语句
try:finally语句不管有没有异常他都会执行:他就是用来清理的try: h=open("ll","r") y=h.read() print (int(y) ...
- Android 中常用的布局
一.线性布局----LinearLayout horizontal 水平 <?xml version="1.0" encoding="utf-8"?& ...
- 【JavaScript基础学习】关于正则表达式的完整内容
w3cJavaScript RegExp对象 这个如果第一次看的话应该会很莫名其妙,但可以看一遍留个印象. 正则表达式30分钟入门教程 这个教程非常完整,走一遍大概能够明白怎么回事了. 正则表达式在 ...
- DSP using MATLAB 示例Example3.1 3.2 3.3
上代码: w = [0:1:500]*pi/500; % [0,pi] axis divided into 501 points. X = exp(j*w) ./ (exp(j*w) - 0.5*on ...
- css:删除:×的效果
常常要使用的显示删除效果: DEMO
- Nico Nico Ni~(完全背包)
Time Limit:2000MS Memory Limit:65535K Type: Program Language: Not Limited Description Lys plays L ...
- HDU-2159FATE(二维完全背包)
FATE Problem Description 最 近xhd正在玩一款叫做FATE的游戏,为了得到极品装备,xhd在不停的杀怪做任务.久而久之xhd开始对杀怪产生的厌恶感,但又不得不通过杀怪来升完 ...