HDU 1024 Max Sum Plus Plus(m个子段的最大子段和)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1024
Max Sum Plus Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 35988 Accepted Submission(s): 12807
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. ^_^
Process to the end of file.
2 6 -1 4 -2 3 -2 3
8
Huge input, scanf and dynamic programming is recommended.
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<algorithm>
using namespace std;
typedef long long LL;
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]={{,},{,-},{,},{-,}}; #define INF 0x7fffffff//无穷大
#define max_v 1000010
int a[max_v];
int now[max_v];// now[j]:包含第j个元素的最大和
int pre[max_v];// pre[j]:前j个元素的最大和,不包括第j个元素
int main()
{
int n,m,maxx;
while(~scanf("%d %d",&m,&n))
{
for(int i=;i<=n;i++)
scanf("%d",&a[i]);
memset(now,,sizeof(now));
memset(pre,,sizeof(pre));
for(int i=;i<=m;i++)//m个子段
{
maxx=-INF;
for(int j=i;j<=n;j++)
{
now[j]=max(now[j-]+a[j],pre[j-]+a[j]);
//now[j] 有两种来源,一种是直接在第i个子段后面加a[j],一种是a[j]单独成为一个子段
pre[j-]=maxx;//更新pre使得pre[j-1]是前j-1个中的最大子段和
maxx=max(now[j],maxx);
}
}
printf("%d\n",maxx);
}
return ;
}
HDU 1024 Max Sum Plus Plus(m个子段的最大子段和)的更多相关文章
- HDU 1024 Max Sum Plus Plus【动态规划求最大M子段和详解 】
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU 1024 Max Sum Plus Plus (动态规划、最大m子段和)
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU 1024 Max Sum Plus Plus【DP,最大m子段和】
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1024 题意: 给定序列,给定m,求m个子段的最大和. 分析: 设dp[i][j]为以第j个元素结尾的 ...
- HDU 1024 Max Sum Plus Plus --- dp+滚动数组
HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...
- HDU 1024 Max Sum Plus Plus (动态规划)
HDU 1024 Max Sum Plus Plus (动态规划) Description Now I think you have got an AC in Ignatius.L's "M ...
- 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 DP
Max Sum Plus Plus Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php ...
- 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 1024 Max Sum Plus Plus【DP】
Now I think you have got an AC in Ignatius.L's "Max Sum" problem. To be a brave ACMer, we ...
随机推荐
- java_对象序列化、反序列化
1.概念 序列化:将对象转化为字节序列的过程 反序列化:将字节序列转化为对象的过程 用途: A:将对象转化为字节序列保存在硬盘上,如文件中,如文本中的例子就是将person对象序列化成字节序列,存在p ...
- spring 代理
java动态代理实现 1. Java自带的动态代理,反射生成字节码 2. Cglib调用asm生成子类 spring 中代理实现 1. 如果类实现了接口,使用java动态代理 2. 没有实现接口,使用 ...
- python __new__()分析
我们来看下下面类中对__new__()方法的实现: class Demo(object): def __init__(self): print '__init__() called...' def _ ...
- Spring课程 Spring入门篇 5-6 introductions应用
1 解析 1.1 aop:declare-parents 标签简介 1.2 标签使用样式 2 代码演练 2.1 introductions标签应用 1 解析 1.1 aop:declare-paren ...
- JavaScript里面的居民们3-去除空格和重复
如代码,分别实现 diyTrim 及 removeRepetition 函数,并跑通代码中的测试用例. <!DOCTYPE html> <html> <head> ...
- 前端学习之路之CSS (三)
Infi-chu: http://www.cnblogs.com/Infi-chu/ 创建CSS有三种方法:外部样式表.内部样式表.内联样式.优先级:内联样式>内部样式>外部样式表> ...
- 【One Day菜鸟到大鸟】MyBatis搭建环境
一.概述 MyBatis是一个持久化框架和Hiberante差不多.比起JDBC来说MyBatis封装了JDBC让我们能够面向对象开发.比起Hiberante来说卸下了Hiberante那种重 ...
- MySQL数据库(5)----删除或更新已有行
有时候,会需要删除某些行,或者修改其内容.这是候便需要用到DELETE语句和UPDATE语句. 1. DELETE 语句的基本格式如下所示: DELETE FROM tbl_name WHERE wh ...
- WinAPI: GetCurrentThread、GetCurrentThreadId、GetCurrentProcess、GetCurrentProcessId
原文:http://www.cnblogs.com/del/archive/2008/03/10/1098311.html {返回当前线程的虚拟句柄} GetCurrentThread: THandl ...
- C++格式化代码,去掉vs2010编辑器里中文注释的红色波浪线
原文:http://sulianqi.cn/Article/ART2013053100001.html Vs2010中C++没有智能感应提示,不习惯,于是装了个番茄插件(Visual Assist x ...