Max Sum Plus Plus

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 25253    Accepted Submission(s): 8703

Problem 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 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
 
Sample Output
6
8
#include <cstdio>
#include <cstring>
#define Max(a,b) ((a>b)?(a):(b))
using namespace std;
const int MAXN=;
const int INF=0x3f3f3f3f;
int dp[MAXN],pre[MAXN],a[MAXN];
int m,n;
int main()
{
while(scanf("%d%d",&m,&n)!=EOF)
{
memset(dp,,sizeof(dp));
memset(pre,,sizeof(pre));
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
int mx;
for(int i=;i<=m;i++)
{
mx=-INF;
for(int j=i;j<=n;j++)
{
dp[j]=Max(dp[j-],pre[j-])+a[j];
pre[j-]=mx;
mx=Max(dp[j],mx);
}
}
printf("%d\n",mx);
}
return ;
}

HDOJ1024(最大M子段和)的更多相关文章

  1. HDOJ-1024(动态规划+滚动数组)

    Max Sum Plus Plus HDOJ-1024 动态转移方程:dp[i][j]=max(dp[i][j-1]+a[j],max(dp[i-1][k])+a[j]) (0<k<j) ...

  2. 最大子段和(c++)

    // 最大子段和.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using namesp ...

  3. 51Node 1065----最小正子段和

    51Node  1065----最小正子段和 N个整数组成的序列a[1],a[2],a[3],…,a[n],从中选出一个子序列(a[i],a[i+1],…a[j]),使这个子序列的和>0,并且这 ...

  4. 最大M子段和 V2

    51nod1053 这题还是我们熟悉的M子段和,只不过N,M<=50000. 这题似乎是一个堆+链表的题目啊 开始考虑把所有正数负数锁在一起. 比如: 1 2 3 -1 –2 -3 666 缩成 ...

  5. 51nod 循环数组最大子段和

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1050 对于普通的数组,只要求一次最大子段和即可.但是这题是可以循环的,所 ...

  6. [日常训练]最大M子段和

    Description 在长度为的序列中选出段互不相交的子段,求最大字段和. Input 第一行两个整数. 第二行个整数. Output 一行一个整数表示最大值. Sample Input 5 2 1 ...

  7. 51nod1049(计算最大子段和)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1049 题意:又是仲文题诶- 思路:暴力会超时,又好像没什么专门 ...

  8. XCOJ 1103 (LCA+树链最大子段和)

    题目链接: http://xcacm.hfut.edu.cn/problem.php?id=1103 题目大意:链更新.链查询,求树链的最大子段和.(子段可以为空) 解题思路: 将所有Query离线存 ...

  9. 洛谷P1121 环状最大两段子段和

    题目描述 给出一段环状序列,即认为A[1]和A[N]是相邻的,选出其中连续不重叠且非空的两段使得这两段和最大. 输入输出格式 输入格式: 输入文件maxsum2.in的第一行是一个正整数N,表示了序列 ...

随机推荐

  1. mac查看网页时翻页

    1 fn + 上下  翻页 2 command + 上下  一下子到文档头和文档尾部

  2. 【python】-- web开发之jQuery

    jQuery jQuery 是一个 JavaScript 函数库,jQuery库包含以下特性(HTML 元素选取.HTML 元素操作.CSS 操作.HTML 事件函数.JavaScript 特效和动画 ...

  3. 修改maven的war包生成路径

    因为要配合jenkins,所以控制了war包的生成目录: <plugins> <!--打war包到指定的目录下 --> <plugin> <groupId&g ...

  4. Meeting-in-the-Middle (LA 2965)

    Meeting-in-the-Middle,又称“中途相遇法”.准确地说,它只是一种策略. 顺便说一下,这个算法真的很冷门! 结合这道题来讨论一下吧:LA 2965.ε(┬┬﹏┬┬)3 因为博主的英文 ...

  5. 坑爹的Hibernate 映射文件错误提示org.xml.sax.SAXParseException

    今天整整一个上午都在和hibernate做斗争,早上一来,继续昨天的项目开发,发现spring项目不能启动,从错误中看是hibernate错误,多半是hibernate配置有错误,关键是错误提示中显示 ...

  6. awk substr()函数

    awk 里的substr函数用法举例: 要截取要截取的内容1: F115!16201!1174113017250745 10.86.96.41 211.140.16.1 200703180718F12 ...

  7. <a href

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEnco ...

  8. cpu架构

    转自 http://blog.csdn.net/wyzxg/article/details/5027738 CPU架构 Architecture ,结构.架构,这个词用于 CPU 的时候是指 CPU ...

  9. cocos2d-x3.0rc打包apk遇到的一些问题记录

    下载cocos2d-x3.0rc后根据官方教程进行环境配置等等一系列过程没有遇到什么问题 打包apk时出现一些问题: 按照官方教程cmd下运行cocos run -p android -m relea ...

  10. Fireworks(whole page)

    <!DOCTYPE HTML> <html> <head> <title>Canvas 实现放烟花特效</title> <meta c ...