HDU1024 最大m子段和
Max Sum Plus Plus
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27582 Accepted Submission(s): 9617
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.
Huge input, scanf and dynamic programming is recommended.
题意:
求最大m子段和。
代码:
//最大m子段和递推公式:dp[i][j]=max(dp[i][j-1]+num[j],dp[i-1][t]+num[j]) (i<=t<=j-1)
//优化:因为每次都要求一个最大的dp[i-1][t](i<=t<=j-1),可以每次求出来dp[i][j]时保存
//这个值,到下一次时直接用就行了,这样每次就只用到了dp[i][j]和dp[i][j-1],可以省去一维。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=;
const int inf=0x7fffffff;
int dp[maxn],fdp[maxn],num[maxn];
int main()
{
int m,n,maxnum;
while(scanf("%d%d",&m,&n)==){
for(int i=;i<=n;i++) scanf("%d",&num[i]);
memset(dp,,sizeof(dp));
memset(fdp,,sizeof(fdp));
for(int i=;i<=m;i++){
maxnum=-inf;
for(int j=i;j<=n;j++){
dp[j]=max(dp[j-]+num[j],fdp[j-]+num[j]);
fdp[j-]=maxnum;
maxnum=max(maxnum,dp[j]);
}
}
printf("%d\n",maxnum);
}
return ;
}
HDU1024 最大m子段和的更多相关文章
- HDU1024 最大M子段和问题 (单调队列优化)
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- 【题解】最大 M 子段和 Max Sum Plus Plus [Hdu1024] [51nod1052]
[题解]最大 M 子段和 Max Sum Plus Plus [Hdu1024] [51nod1052] 传送门:最大 \(M\) 子段和 \(Max\) \(Sum\) \(Plus\) \(Plu ...
- HDU1024(最大M子段和)
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- HDU1024 DP的优化 最大M子段和问题
Max Sum Plus Plus Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- m大子段和 hdu1024
给出n个数,m个区间: 求选区m个区间的最大值: #include<cstdio> #include<algorithm> #include<math.h> #in ...
- 最大m段子段和
hdu1024 最大m子序列和 给定你一个序列,让你求取m个子段(不想交的子段)并求取这m个子段和的最大值 从二维开始来看dp[i][j]表示取第j个数作为第i个子段的元素所得到的前i个子段和的最大值 ...
- 最大子段和(c++)
// 最大子段和.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using namesp ...
- hdu1024 dp
题意:求一个序列中的最大 m 段和,m 段不能交叉. dp[i][0/1][j] 表示已经取完第 i 个物品,第 i 个物品取或不取,取到第 j 个子段. 用vis[i][0/1][j] 表示该 dp ...
- 51Node 1065----最小正子段和
51Node 1065----最小正子段和 N个整数组成的序列a[1],a[2],a[3],…,a[n],从中选出一个子序列(a[i],a[i+1],…a[j]),使这个子序列的和>0,并且这 ...
随机推荐
- [Clr via C#读书笔记]Cp1CLR执行模型
Cp1CLR执行模型 本章的概念点 CLR=Common Language Runtime 内存管理,程序集加载,安全性,异常处理和线程同步.CLR是基础,支持着面向它的各种语言.各种语言会被对应的编 ...
- ionic LoadingController 模块使用
html 代码: <ion-header> <ion-navbar> <ion-title>Loading</ion-title> </ion-n ...
- ubuntu ssh配置
Secure Shell (SSH) is a cryptographic network protocol for operating network services securely over ...
- Python3 Tkinter-Toplevel
1.创建 Toplevel与Frame类似,但是它包含窗体属性(如Title) from tkinter import * root=Tk() tl=Toplevel() Label(tl,text= ...
- POJ 3028 Shoot-out(概率DP)
Description This is back in the Wild West where everybody is fighting everybody. In particular, ther ...
- HDU 3260/POJ 3827 Facer is learning to swim(DP+搜索)(2009 Asia Ningbo Regional)
Description Facer is addicted to a game called "Tidy is learning to swim". But he finds it ...
- svn服务器 备份,迁移,部署方案
这次做业务迁移,要从一个云厂商迁移到某云厂商,之前每天到全备svn排到用场了,需要搭建一个全新到svn服务并要做迁移,并实现我们开发机到时时代码同步 一.svn备份有很多种,优劣都不同,百度可查,我采 ...
- Python中的名字隐藏
Python对于module文件中的name是没有private和public区分的,严格来说,在module文件重定义的任何name,都可以被外界访问.但是,对于 from module imort ...
- Python中的Numeric
整型Integer 在Python2.X中,Integer有两种类型,一种是32bit的普通类型,一种是精度无限制的long类型,在数字后面标识l或者L来标识long类型,并且,当32bit发生ove ...
- 团队协作第八周个人PSP
11.3 --11.9本周例行报告 1.PSP(personal software process )个人软件过程. 类型 任务 开始时间 结束时间 中断时间 实际用时 ...