HDU 2829 Lawrence
$dp$,斜率优化。
设$dp[i][j]$表示前$i$个数字切了$j$次的最小代价。$dp[i][j]=dp[k][j-1]+p[k+1][i]$。观察状态转移方程,可以发现是一列一列推导出来的。可以初始化第一列,然后算第二列,然后算第三列。
暴力算的话时间复杂度是$O(n^3)$,需要优化。将$p[x][y]$换成带有$sum$的式子,一顿化简后,发现可以斜率优化。具体过程与HDU 3507差不多。不再赘述。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<ctime>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
}
template <class T>
inline void read(T &x)
{
char c = getchar();
x = ;
while(!isdigit(c)) c = getchar();
while(isdigit(c))
{
x = x * + c - '';
c = getchar();
}
} int n,m;
long long a[],sum[],p[][],dp[][];
int f1,f2,q[]; bool delete1(int x,int a,int b,int c)
{
if(dp[b][x]-p[][b]+sum[b]*sum[b]-dp[a][x]+p[][a]-sum[a]*sum[a]<sum[c]*(sum[b]-sum[a])) return ;
return ;
} bool delete2(int x,int a,int b,int c)
{
if((dp[c][x]-p[][c]+sum[c]*sum[c]-dp[b][x]+p[][b]-sum[b]*sum[b])*(sum[b]-sum[a])<
(dp[b][x]-p[][b]+sum[b]*sum[b]-dp[a][x]+p[][a]-sum[a]*sum[a])*(sum[c]-sum[b])) return ;
return ;
} int main()
{
while(~scanf("%d%d",&n,&m))
{
if(n==&&m==) break; m++;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum[i]=sum[i-]+a[i];
}
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
int s=j,e=j+i-;
if(e>n) continue;
if(i==) { p[s][e]=; continue; }
p[s][e]=p[s+][e]+a[s]*(sum[e]-sum[s]);
}
} for(int i=;i<=n;i++) dp[i][]=p[][i]; for(int j=;j<=m;j++)
{
f1=; f2=; q[f2]=j-;
for(int i=j;i<=n;i++)
{
while()
{
if(f2-f1+<) break;
if(delete1(j-,q[f1],q[f1+],i)) f1++;
else break;
} dp[i][j] = dp[q[f1]][j-]+p[q[f1]+][i]; while()
{
if(f2-f1+<) break;
if(delete2(j-,q[f2-],q[f2],i)) f2--;
else break;
} f2++; q[f2]=i;
}
} printf("%lld\n",dp[n][m]);
}
return ;
}
HDU 2829 Lawrence的更多相关文章
- hdu 2829 Lawrence(斜率优化DP)
题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...
- HDU 2829 - Lawrence - [斜率DP]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 T. E. Lawrence was a controversial figure during ...
- HDU 2829 Lawrence(四边形优化DP O(n^2))
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 题目大意:有一段铁路有n个站,每个站可以往其他站运送粮草,现在要炸掉m条路使得粮草补给最小,粮草 ...
- HDU 2829 Lawrence(斜率优化DP O(n^2))
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 题目大意:有一段铁路有n个站,每个站可以往其他站运送粮草,现在要炸掉m条路使得粮草补给最小,粮草 ...
- HDU 2829 Lawrence(动态规划-四边形不等式)
Lawrence Problem Description T. E. Lawrence was a controversial figure during World War I. He was a ...
- hdu 2829 Lawrence(四边形不等式优化dp)
T. E. Lawrence was a controversial figure during World War I. He was a British officer who served in ...
- HDU 2829 Lawrence (斜率DP)
斜率DP 设dp[i][j]表示前i点,炸掉j条边的最小值.j<i dp[i][j]=min{dp[k][j-1]+cost[k+1][i]} 又由得出cost[1][i]=cost[1][k] ...
- HDU.2829.Lawrence(DP 斜率优化)
题目链接 \(Description\) 给定一个\(n\)个数的序列,最多将序列分为\(m+1\)段,每段的价值是这段中所有数两两相乘的和.求最小总价值. \(Solution\) 写到这突然懒得写 ...
- HDU 2829 Lawrence (斜率优化DP或四边形不等式优化DP)
题意:给定 n 个数,要你将其分成m + 1组,要求每组数必须是连续的而且要求得到的价值最小.一组数的价值定义为该组内任意两个数乘积之和,如果某组中仅有一个数,那么该组数的价值为0. 析:DP状态方程 ...
随机推荐
- JavaScript使用数组拼接字符串性能如何?
传统上,字符串连接一直是js中性能最低的操作之一. view source print? 1 var text="Hello"; 2 text+=" World!&q ...
- CentOS 7, Attempting to create directory /root/perl5
By francis_hao Apr 10,2017 在使用CentOS 7的时候,首次登陆会出现新建一个perl5文件夹的提示,删除该文件后,之后登陆还是会出现该提示并新建了perl5文件夹. ...
- sshd_conf配置
# $OpenBSD: sshd_config,v 1.80 2008/07/02 02:24:18 djm Exp $ # This is the sshd server system-w ...
- poj 1523 割点 tarjan
Description Consider the two networks shown below. Assuming that data moves around these networks on ...
- [ZJOI2005]午餐 (DP)
[ZJOI2005]午餐 题目描述 上午的训练结束了,THU ACM小组集体去吃午餐,他们一行N人来到了著名的十食堂.这里有两个打饭的窗口,每个窗口同一时刻只能给一个人打饭.由于每个人的口味(以及胃口 ...
- There is an overlap in the region chain
ERROR: (regions day_hotstatic,860010-2355010000_20140417_12_entry_00000000321,1398674475358.0dc20573 ...
- Installing Jenkins to Centos Docker
1.Install Docker CE to Centos7 [root@zoo1 ~]# yum install -y yum-utils device-mapper-persistent-data ...
- HTML5学习之新增标签
转自:http://www.cnblogs.com/fly_dragon/archive/2012/05/25/2516142.html 作者:FlyDragon 一.引言 在本节中,笔者将向大家讲述 ...
- ORACLE中根据生日得到年龄
create or replace function F_GETAGE(dateofbirth date) return varchar2 is begin ) then ); else ) then ...
- codeforces739C - Skills &&金中市队儿童节常数赛
http://codeforces.com/problemset/problem/739/C 先上链接 这道题 对于蒟蒻的我来说还是很有难度的 调了很久 对于我的代码 mx2是答案 mx1代表单调 m ...