POJ 1160 Post Office (四边形不等式优化DP)
题意: 给出m个村庄及其距离,给出n个邮局,要求怎么建n个邮局使代价最小。
析:一般的状态方程很容易写出,dp[i][j] = min{dp[i-1][k] + w[k+1][j]},表示前 j 个村庄用 k 个邮局距离最小,w可以先预处理出来O(n^2),但是这个方程很明显是O(n^3),但是因为是POJ,应该能暴过去。。= =,正解应该是对DP进行优化,很容易看出来,w是满足四边形不等式的,也可以推出来 s 是单调的,可以进行优化。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e16;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 300 + 10;
const int mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
} int dp[35][maxn], w[maxn][maxn], s[35][maxn];
int a[maxn]; int main(){
while(scanf("%d %d", &n, &m) == 2){
for(int i = 1; i <= n; ++i) scanf("%d", a+i);
for(int i = 1; i <= n; ++i){
w[i][i] = 0;
for(int j = i+1; j <= n; ++j)
w[i][j] = w[i][j-1] + a[j] - a[i+j>>1];
}
memset(dp, INF, sizeof dp);
memset(s, 0, sizeof s);
for(int i = 1; i <= n; ++i) dp[1][i] = w[1][i];
for(int i = 2; i <= m; ++i){
s[i][n+1] = n;
for(int j = n; j >= i; --j)
for(int k = s[i-1][j]; k <= s[i][j+1]; ++k)
if(dp[i][j] > dp[i-1][k] + w[k+1][j]){
dp[i][j] = dp[i-1][k] + w[k+1][j];
s[i][j] = k;
}
}
printf("%d\n", dp[m][n]);
}
return 0;
}
POJ 1160 Post Office (四边形不等式优化DP)的更多相关文章
- 【转】斜率优化DP和四边形不等式优化DP整理
(自己的理解:首先考虑单调队列,不行时考虑斜率,再不行就考虑不等式什么的东西) 当dp的状态转移方程dp[i]的状态i需要从前面(0~i-1)个状态找出最优子决策做转移时 我们常常需要双重循环 (一重 ...
- hdu 2829 Lawrence(四边形不等式优化dp)
T. E. Lawrence was a controversial figure during World War I. He was a British officer who served in ...
- BZOJ1563/洛谷P1912 诗人小G 【四边形不等式优化dp】
题目链接 洛谷P1912[原题,需输出方案] BZOJ1563[无SPJ,只需输出结果] 题解 四边形不等式 什么是四边形不等式? 一个定义域在整数上的函数\(val(i,j)\),满足对\(\for ...
- codevs3002石子归并3(四边形不等式优化dp)
3002 石子归并 3 参考 http://it.dgzx.net/drkt/oszt/zltk/yxlw/dongtai3.htm 时间限制: 1 s 空间限制: 256000 KB 题目等级 ...
- CF321E Ciel and Gondolas Wqs二分 四边形不等式优化dp 决策单调性
LINK:CF321E Ciel and Gondolas 很少遇到这么有意思的题目了.虽然很套路.. 容易想到dp \(f_{i,j}\)表示前i段分了j段的最小值 转移需要维护一个\(cost(i ...
- POJ 1160 四边形不等式优化DP Post Office
d(i, j)表示用i个邮局覆盖前j个村庄所需的最小花费 则有状态转移方程:d(i, j) = min{ d(i-1, k) + w(k+1, j) } 其中w(i, j)的值是可以预处理出来的. 下 ...
- HDU 2829 Lawrence (斜率优化DP或四边形不等式优化DP)
题意:给定 n 个数,要你将其分成m + 1组,要求每组数必须是连续的而且要求得到的价值最小.一组数的价值定义为该组内任意两个数乘积之和,如果某组中仅有一个数,那么该组数的价值为0. 析:DP状态方程 ...
- 四边形不等式优化DP——石子合并问题 学习笔记
好方啊马上就要区域赛了连DP都不会QAQ 毛子青<动态规划算法的优化技巧>论文里面提到了一类问题:石子合并. n堆石子.现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆石子合并成新的 ...
- BZOJ 1010 玩具装箱toy(四边形不等式优化DP)(HNOI 2008)
Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1... ...
随机推荐
- 七、Jmeter + ant + jenkins轻量级接口自动化测试
七.Jmeter + ant + jenkins轻量级接口自动化测试 杀猪不用牛刀,工具没有牛逼高大尚之分,每个工具都有存在的理由:关键是看会不会用,怎么用,有没有用在合适的地方. 需要安装的工具: ...
- 在actionbar中加入item的方法
首先在menu文件夹中创建post.xml <?xml version="1.0" encoding="utf-8"?> <menu xmln ...
- linux 系统创建软连接
ln -s /data/var/ /usr/local/smokeping/var 需求:/var/本身在/usr/local/smokeping/var下,想要把/usr/local/smokepi ...
- emqtt 3 (我要subscribe 这个topic)
这一次,主要分析client subscribe 某个topic 的处理流程. 由protocol开始 是的,还是要从protocol开始,至于为什么,之前就说过了. subscribe 类型的pac ...
- NOIP2005普及组第3题 采药 (背包问题)
NOIP2005普及组第3题 采药 时间限制: 1 Sec 内存限制: 128 MB提交: 50 解决: 23[提交][状态][讨论版][命题人:外部导入] 题目描述 辰辰是个天资聪颖的孩子,他的 ...
- php去除html
代码如下 //清除html function clearhtml($str){ $str = trim($str); $str = strip_tags($str,""); $st ...
- Spring Cloud与分布式系统
本文不是讲解如何使用spring Cloud的教程,而是探讨Spring Cloud是什么,以及它诞生的背景和意义. 背景 2008年以后,国内互联网行业飞速发展,我们对软件系统的需求已经不再是过去” ...
- python中常用模块详解一
1.time 模块 import time s = time.localtime() # 把时间转化成格式化的时间,通过. 取得里面的年月日等 struct_time 格式 time.struct_t ...
- IDA Pro 权威指南学习笔记(二) - IDA 数据库文件
生成数据库文件 把要分析的文件用 IDA 打开后,会生成 3 个数据库文件 扩展名分别为 .id0,id1,nam .id0 文件是一个二叉树形式的数据库 .id1 文件包含描述每个程序字节的标记 . ...
- PHP字符串的处理(三)-字符串的输出
1.echo() echo()实际不是一个函数,是一个语言结构,不需要使用括号 <?php $str = "test"; echo $str."<br> ...