51nod1052 最大M子段和
dp优化我总是不太熟练。这一次首先我写了O(n4)->O(n3)->O(n2)。一步步的优化过来。yyl好像用的是单调队列优化dp我看不懂他的代码。。。
O(n4)
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
#define ll long long
ll read(){
ll x=0,f=1;char c=getchar();
while(!isdigit(c)) {
if(c=='-') f=-1;c=getchar();
}
while(isdigit(c)) x=x*10+c-'0',c=getchar();
return x*f;
}
const int nmax=5e3+5;
const ll inf=1e18;
ll dp[nmax],g[nmax],sm[nmax];
int main(){
int n=read(),m=read();ll u,v,d;
rep(i,1,n) sm[i]=sm[i-1]+read();
rep(i,1,m) {
rep(j,i,n) {
rep(k,0,j-1) {
u=inf;
rep(t,k+1,j) u=min(u,sm[t]);
dp[j]=max(dp[j],g[k]+sm[j]-u);
}
}
rep(j,1,n) g[j]=dp[j];
}
ll ans=0;
rep(i,m,n) ans=max(ans,dp[i]);
printf("%lld\n",ans);
return 0;
}
O(n3)
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
#define ll long long
ll read(){
ll x=0,f=1;char c=getchar();
while(!isdigit(c)) {
if(c=='-') f=-1;c=getchar();
}
while(isdigit(c)) x=x*10+c-'0',c=getchar();
return x*f;
}
const int nmax=5e3+5;
const ll inf=1e18;
ll dp[nmax],g[nmax],sm[nmax];
int main(){
int n=read(),m=read();ll u,v,d,tm=0,cnt=0;
rep(i,1,n) {
sm[i]=sm[i-1]+(u=read());
if(u) ++cnt,tm+=u;
}
if(m>=cnt) {
printf("%lld\n",tm);return 0;
}
rep(i,1,m) {
rep(j,i,n) {
dp[j]=dp[j-1];
rep(k,0,j-1) dp[j]=max(dp[j],g[k]+sm[j]-sm[k]);
}
rep(j,1,n) g[j]=dp[j];
}
printf("%lld\n",dp[n]);
return 0;
}
O(n2)
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
using namespace std;
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
#define ll long long
ll read(){
ll x=0,f=1;char c=getchar();
while(!isdigit(c)) {
if(c=='-') f=-1;c=getchar();
}
while(isdigit(c)) x=x*10+c-'0',c=getchar();
return x*f;
}
const int nmax=5e3+5;
const ll inf=1e18;
ll dp[nmax],sm[nmax],f[nmax];
int main(){
int n=read(),m=read();ll u,v,d,tm=0,cnt=0;
rep(i,1,n) {
sm[i]=sm[i-1]+(u=read());
if(u) ++cnt,tm+=u;
}
if(m>=cnt) {
printf("%lld\n",tm);return 0;
}
f[0]=-inf;rep(j,1,n) f[j]=max(f[j-1],dp[j-1]-sm[j-1]);
rep(i,1,m) {
rep(j,i,n) dp[j]=max(dp[j-1],f[j]+sm[j]);
f[0]=-inf;rep(j,1,n) f[j]=max(f[j-1],dp[j-1]-sm[j-1]);
}
printf("%lld\n",dp[n]);
return 0;
}


第1行:2个数N和M,中间用空格分隔。N为整数的个数,M为划分为多少段。(2 <= N , M <= 5000)
第2 - N+1行:N个整数 (-10^9 <= a[i] <= 10^9)
输出这个最大和
7 2
-2
11
-4
13
-5
6
-2
26
51nod1052 最大M子段和的更多相关文章
- 【题解】最大 M 子段和 Max Sum Plus Plus [Hdu1024] [51nod1052]
[题解]最大 M 子段和 Max Sum Plus Plus [Hdu1024] [51nod1052] 传送门:最大 \(M\) 子段和 \(Max\) \(Sum\) \(Plus\) \(Plu ...
- 51nod 最大M子段和系列(1052、1053、1115)
51nod1052 数据量小,可使用O(N*M)的DPAC,递推公式: dp[i][j]=max(dp[i-1][j-1], dp[i][j-1])+a[j]; dp[i][j]表示前j个数取 i 段 ...
- 最大子段和(c++)
// 最大子段和.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using namesp ...
- 51Node 1065----最小正子段和
51Node 1065----最小正子段和 N个整数组成的序列a[1],a[2],a[3],…,a[n],从中选出一个子序列(a[i],a[i+1],…a[j]),使这个子序列的和>0,并且这 ...
- 最大M子段和 V2
51nod1053 这题还是我们熟悉的M子段和,只不过N,M<=50000. 这题似乎是一个堆+链表的题目啊 开始考虑把所有正数负数锁在一起. 比如: 1 2 3 -1 –2 -3 666 缩成 ...
- 51nod 循环数组最大子段和
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1050 对于普通的数组,只要求一次最大子段和即可.但是这题是可以循环的,所 ...
- [日常训练]最大M子段和
Description 在长度为的序列中选出段互不相交的子段,求最大字段和. Input 第一行两个整数. 第二行个整数. Output 一行一个整数表示最大值. Sample Input 5 2 1 ...
- 51nod1049(计算最大子段和)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1049 题意:又是仲文题诶- 思路:暴力会超时,又好像没什么专门 ...
- XCOJ 1103 (LCA+树链最大子段和)
题目链接: http://xcacm.hfut.edu.cn/problem.php?id=1103 题目大意:链更新.链查询,求树链的最大子段和.(子段可以为空) 解题思路: 将所有Query离线存 ...
随机推荐
- Ckeditor 中一些核心的对象的作用
1.CKEditor CKEditor对象用于掌管全局,他是一个单例对象,管理着所有实例化了的编辑框. 通过replace方法创建编辑框实例. 2.CKEditor.editor 表示一个编辑框实例, ...
- PHP几个函数
pack: 数据装入一个二进制字符串 http_build_query: 将数组转化成URL GET参数的形式. get_class:返回对象的类名,注:即使是在父类方法中调用也是返回子类的类名. g ...
- 你必须知道的ADO.NET
原文:http://www.cnblogs.com/liuhaorain/archive/2012/02/06/2340409.html 1. 什么是ADO.NET? 简单的讲,ADO.NET是一组允 ...
- java Timer类
java.util 类 Timer java.lang.Object java.util.Timer public class Timerextends Object 一种工具,线程用其安排以后在后台 ...
- javascript 图片延迟加载
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- poj 1797(最短路变形)
题目链接:http://poj.org/problem?id=1797 思路:题目意思很简单,n个顶点,m条路,每条路上都有最大载重限制,问1->n最大载重量.其实就是一最短路的变形,定义wei ...
- sql server 2008 评估期已过期解决办法
开始-->所有程序-->Microsoft SQL Server 2008-->配置工具-->SQL Server 安装中心-->维护-->版本升级,接着按照提示一 ...
- web工程导入MyEclipse 就变成Java工程 ———— 解决方案
Web 工程 导入到 MyEclipse 中后就变成 Java工程了 折腾大大半天,最后才发现是 .settings 里面文件的配置问题.. .settings 文件夹里面的 org.eclipse. ...
- 【uva753/poj1087/hdu1526-A Plug for UNIX】最大流
题意:给定n个插座,m个插头,k个转换器(x,y),转换器可以让插头x转成插头y.问最少有多少个插头被剩下. 题解: 最大流或者二分图匹配.然而我不知道怎么打二分图匹配..打了最大流.这题字符串比较坑 ...
- Linux搜狗输入法在有道云笔记上输入冗余
Linux下,在有道云笔记中用搜狗拼音输入法时,会出现输入冗余,类似于输入法的缓冲上屏了.这是有道云笔记Web页面的问题. 暂时的解决办法是按Ctrl + alt + p. 不仅搜狗输入法,凡是用fc ...