HDU 2829 斜率优化DP Lawrence
题意:n个数之间放m个障碍,分隔成m+1段。对于每段两两数相乘再求和,然后把这m+1个值加起来,让这个值最小。
设:
d(i, j)表示前i个数之间放j个炸弹能得到的最小值
sum(i)为前缀和,cost(i)为前i个数两两相乘之和。
则有状态转移方程:
设0 ≤ l < k < i,且k比l更优,有不等式:
整理得到,注意不等号方向:
最后变成了斜率的形式,下面就用一个队列维护即可。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + ; int n, m; int sum[maxn], cost[maxn];
int d[maxn][maxn]; int head, tail;
int Q[maxn]; int j; int inline Y(int i)
{
return d[i][j-] + sum[i] * sum[i] - cost[i];
} int inline DY(int x1, int x2) { return Y(x2) - Y(x1); } int inline DX(int x1, int x2) { return sum[x2] - sum[x1]; } int inline DP(int x1, int x2) { return d[x1][j-] + cost[x2] - cost[x1] - sum[x1]*(sum[x2]-sum[x1]); } int main()
{
while(scanf("%d%d", &n, &m) == && n)
{
for(int i = ; i <= n; i++) scanf("%d", sum + i);
for(int i = ; i <= n; i++) sum[i] += sum[i - ];
for(int i = ; i <= n; i++) cost[i] = cost[i-] + (sum[i]-sum[i-])*sum[i-]; for(int i = ; i <= n; i++) d[i][] = cost[i];
for(j = ; j <= m; j++)
{
head = tail = ;
Q[tail++] = ;
for(int i = ; i <= n; i++)
{
while(head + < tail && DY(Q[head], Q[head+]) <= sum[i] * DX(Q[head], Q[head+])) head++;
d[i][j] = DP(Q[head], i);
while(head + < tail && DY(Q[tail-], i) * DX(Q[tail-], Q[tail-]) <= DY(Q[tail-], Q[tail-]) * DX(Q[tail-], i)) tail--;
Q[tail++] = i;
}
} printf("%d\n", d[n][m]);
} return ;
}
代码君
贴一个四边形不等式优化的代码对比一下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int maxn = + ;
const int INF = 0x3f3f3f3f; int n, m;
int a[maxn], sum[maxn], cost[maxn];
int d[maxn][maxn], s[maxn][maxn]; int inline w(int k, int j)
{
return cost[j] - cost[k-] - sum[k-] * (sum[j] - sum[k-]);
} int main()
{
while(scanf("%d%d", &n, &m) == && n)
{
m++; for(int i = ; i <= n; i++) scanf("%d", a + i);
for(int i = ; i <= n; i++) sum[i] = sum[i - ] + a[i];
for(int i = ; i <= n; i++) cost[i] = cost[i-] + sum[i-] * a[i]; memset(d, , sizeof(d));
for(int i = ; i <= m; i++)
for(int j = i + ; j <= n; j++) d[i][j] = INF; for(int i = ; i <= n; i++) { d[][i] = cost[i]; s[][i] = ; }
for(int i = ; i <= m; i++)
{
s[i][n+] = n;
for(int j = n; j >= i; j--)
{
for(int k = s[i-][j]; k <= s[i][j+]; k++)
{
int tmp = d[i-][k] + w(k+, j);
if(tmp < d[i][j])
{ d[i][j] = tmp; s[i][j] = k; }
}
}
} printf("%d\n", d[m][n]);
} return ;
}
代码君
HDU 2829 斜率优化DP Lawrence的更多相关文章
- hdu 3669(斜率优化DP)
Cross the Wall Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 327680/327680 K (Java/Others) ...
- HDU 4258 斜率优化dp
Covered Walkway Time Limit: 30000/10000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Othe ...
- hdu 3045 斜率优化DP
思路:dp[i]=dp[j]+sum[i]-sum[j]-(i-j)*num[j+1]; 然后就是比较斜率. 注意的时这里j+t<=i: #include<iostream> #in ...
- Print Article HDU - 3507 -斜率优化DP
思路 : 1,用一个单调队列来维护解集. 2,假设队列中从头到尾已经有元素a b c.那么当d要入队的时候,我们维护队列的下凸性质, 即如果g[d,c]<g[c,b],那么就将c点删除.直到找到 ...
- HDU 3507 斜率优化dp
Print Article Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)To ...
- HDU 3507斜率优化dp
Print Article Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)To ...
- HDU 3507 斜率优化 DP Print Article
在kuangbin巨巨博客上学的. #include <iostream> #include <cstdio> #include <cstring> #includ ...
- hdu 2829 Lawrence(斜率优化DP)
题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...
- HDU 2829 Lawrence (斜率优化DP或四边形不等式优化DP)
题意:给定 n 个数,要你将其分成m + 1组,要求每组数必须是连续的而且要求得到的价值最小.一组数的价值定义为该组内任意两个数乘积之和,如果某组中仅有一个数,那么该组数的价值为0. 析:DP状态方程 ...
随机推荐
- 去除List<Object>集合中重复的元素(利用HashSet的特性---无重复元素)
import java.util.ArrayList;import java.util.HashSet;import java.util.Iterator; public class Hashset ...
- 关于controller返回的页面js文件和css文件404问题的正式解决
谨用此博客记录一下这条卡了两个星期的bug…… 还是之前的问题,通过get方法,后台@Controller返回页面然后弹窗.但是不知道为什么一直所有js文件和css文件都报404…… (之前的博客记录 ...
- js 中对字符串的操作
1.split() split() 方法用于把一个字符串分割成字符串数组. 用法:stringObject.split(separator,howmany) separator:必选,类型为字符串或者 ...
- Oracle 恢复数据后,数据库中中文变成问号解决方法
1.右击---我的电脑---环境变量 2.新增环境变量 变量名:LANG=zh_CN.GBK NLS_LANG=SIMPLIFIED CHINESE_CHINA.ZHS16GBK 3.重启PLSQL或 ...
- 接口文档管理工具rap
git地址: https://github.com/thx/RAP wiki : https://github.com/thx/RAP/wiki/home_cn 视频教程: http://thx.g ...
- 访问者模式和php实现
访问者模式: 表示作用于某个对象结构中的各个元素的操作.它使你可以在不改变各个元素类的前提下定义作用于这些元素的操作. 角色: 1)抽象访问者:为该对象结构中具体元素角色声明一个访问操作接口.该操作接 ...
- HDU4405 Aeroplane chess(期望dp)
题意 抄袭自https://www.cnblogs.com/Paul-Guderian/p/7624039.html 正在玩飞行棋.输入n,m表示飞行棋有n个格子,有m个飞行点,然后输入m对u,v表示 ...
- codeforces736D. Permutations(线性代数)
题意 $m \leqslant 500000$,题目打错了 Sol 神仙题Orz 构造矩阵$B$,使得$B[b[i]][a[i]] = 1$ 那么他的行列式的奇偶性也就对应了生成排列数列数量的奇偶性( ...
- codevs 3054 高精度练习-文件操作
时间限制: 1 s 空间限制: 64000 KB 题目等级 : 钻石 Diamond 题目描述 Description 输入一组数据,将每个数据加1后输出 输入描述 Input Descripti ...
- C++数据文件存储与加载(利用opencv)
首先请先确认已经安装好了opencv3及以上版本. #include <opencv2/opencv.hpp>#include <iostream>#include <s ...