P4767 [IOI2000]邮局 - 平行四边形不等式优化DP
There is a straight highway with villages alongside the highway. The highway is represented as an integer axis, and the position of each village is identified with a single integer coordinate. There are no two villages in the same position. The distance between two positions is the absolute value of the difference of their integer coordinates.
Post offices will be built in some, but not necessarily all of the villages. A village and the post office in it have the same position. For building the post offices, their positions should be chosen so that the total sum of all distances between each village and its nearest post office is minimum.
You are to write a program which, given the positions of the villages and the number of post offices, computes the least possible sum of all distances between each village and its nearest post office.
在高速公路旁边有一条村庄的直道。高速公路表示为整数轴,并且每个村庄的位置用单个整数坐标标识。同一地点没有两个村庄。两个位置之间的距离是它们整数坐标差的绝对值。
邮局将建在一些但不一定是所有村庄。一个村庄和其中的邮局具有相同的位置。为了建造邮局,应选择其位置,以便每个村庄与其最近的邮局之间的所有距离的总和最小。
你要写一个程序,考虑到村庄的位置和邮局的数量,计算每个村庄和最近的邮局之间所有距离的最小可能总和。
Input
Your program is to read from standard input. The first line contains two integers: the first is the number of villages V, 1 <= V <= 300, and the second is the number of post offices P, 1 <= P <= 30, P <= V. The second line contains V integers in increasing order. These V integers are the positions of the villages. For each position X it holds that 1 <= X <= 10000.
此程序是从标准输入读取。第一行包含两个整数:第一行是村庄的数量V,1 <= V <= 300,第二行是邮局的数量P,1 <= P <= 30,P <= V. 第二行按升序包含V个整数。这V个整数是村庄的位置。对于每个位置X,保证 1 <= X <= 10000。
Output
The first line contains one integer S, which is the sum of all distances between each village and its nearest post office.第一行包含一个整数S,它是每个村庄与其最近的邮局之间所有距离的总和。
方法
这道题是一道DP题,由于DP题有一个常见的性质,即 f [ ][ ] 数组中存的子解都具有可输出的共同点,所以这种DP题的状态转移方程都是很好推断的。这道题的 f [ i ] [ j ] 表示前 i 个村庄建 j 个邮局的解,状态转移方程是
由于此复杂度是O(),但是数据量很小,所以用朴素DP是可以做出来的。
但是,为了提升自我增长知识,我决定优化一下,把复杂度降为O(),具体是用一种叫“平行四边形不等式优化DP”的方式,把 K 的枚举平摊成一次O(n)。
判断是否能用此法,主要靠
1) 暴力打表找规律
2) 凭直觉
平行四边形优化主要有一个特点,每一个 f [ i ][ j ] 最后的 k 值都大于 f [ i ][ j - 1 ] 的 k ,且小于 f [ i + 1 ][ j ] 的 k 。意思是,状态转移方程可以优化:
主要是因为 w [][] 具有平行四边形不等式的性质,即对于 a < b < c < d ,
w [ a ] [ c ] + w [ b ] [ d ] < w [ a ] [ d ] + w [ b ] [ c ] 。
这可以算是最难的一种方法,因为坑很多,很难AC,但是一般只要样例过了,就离成功不远了。
详见大神题解:https://blog.csdn.net/NOIAu/article/details/72514812
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
int read() {
int f = 1,x = 0;char s = getchar();
while(s < '0' || s > '9') {if(s == '-')f = -1;s = getchar();}
while(s >= '0' && s <= '9') {x = x * 10 + s - '0';s = getchar();}
return x * f;
}
int n,m,i,j,k,o;
int pos[3005],f[3005][305],w[3005][3005],sum1[3005],sum2[3005],s[3005][3005];
int main() {
n = read();m = read();
for(i = 1;i <= n;i ++) {
pos[i] = read();
}
for(i = 1;i <= n;i ++) {
sum1[i] = sum1[i - 1] + pos[i] - pos[1];
}
for(i = n;i > 0;i --) {
sum2[i] = sum2[i + 1] + pos[n] - pos[i];
}
for(i = 1;i <= n;i ++) {
for(j = i;j <= n;j ++) {
if(i == j) w[i][j] = 0;
else {
int k = (i + j) / 2;
w[i][j] = sum1[j] - sum1[k] - (j - k) * (pos[k] - pos[1]) + sum2[i] - sum2[k] - (k - i) * (pos[n] - pos[k]);
}
}
}
memset(f,0x3f,sizeof(f));
for(i = 1;i <= n;i ++) {
f[i][1] = w[1][i];
}
f[0][0] = f[1][1] = 0;
for(i = 1;i <= n;i ++) {
for(j = min(i,m);j > 0;j --) {
if(!s[i - 1][j]) s[i - 1][j] = min(i - 1,j - 1);
if(!s[i][j + 1]) s[i][j + 1] = i - 1;
for(k = s[i - 1][j];k <= s[i][j + 1];k ++) {
if(f[k][j - 1] + w[k + 1][i] < f[i][j]) {
f[i][j] = f[k][j - 1] + w[k + 1][i];
s[i][j] = k;
}
}
}
}
printf("%d",f[n][m]);
return 0;
}
P4767 [IOI2000]邮局 - 平行四边形不等式优化DP的更多相关文章
- BZOJXXXX: [IOI2000]邮局——四边形不等式优化初探
貌似$BZOJ$上并没有这个题... 是嫌这个题水了么... 还是要氪金权限号??? 这里附上洛谷的题面:洛谷P4767 [IOI2000]邮局 题目描述 高速公路旁边有一些村庄.高速公路表示为整数轴 ...
- 题解——洛谷P4767 [IOI2000]邮局(区间DP)
这题是一道区间DP 思维难度主要集中在如何预处理距离上 由生活经验得,邮局放在中间显然最优 所以我们可以递推求出\( w[i][j] \)表示i,j之间放一个邮局得距离 然后设出状态转移方程 设\( ...
- 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 ...
- 【转】斜率优化DP和四边形不等式优化DP整理
(自己的理解:首先考虑单调队列,不行时考虑斜率,再不行就考虑不等式什么的东西) 当dp的状态转移方程dp[i]的状态i需要从前面(0~i-1)个状态找出最优子决策做转移时 我们常常需要双重循环 (一重 ...
- 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 ...
- HDU 2829 Lawrence (斜率优化DP或四边形不等式优化DP)
题意:给定 n 个数,要你将其分成m + 1组,要求每组数必须是连续的而且要求得到的价值最小.一组数的价值定义为该组内任意两个数乘积之和,如果某组中仅有一个数,那么该组数的价值为0. 析:DP状态方程 ...
- 四边形不等式优化DP——石子合并问题 学习笔记
好方啊马上就要区域赛了连DP都不会QAQ 毛子青<动态规划算法的优化技巧>论文里面提到了一类问题:石子合并. n堆石子.现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆石子合并成新的 ...
随机推荐
- 2021.02.27【NOIP提高B组】模拟 总结
T1 欧拉筛质数时若 \(i\) 是质数且没有被用过就顺便用于计算结果,复杂度 \(O(n)\) #include<bits/stdc++.h> using namespace std; ...
- k8s client-go源码分析 informer源码分析(6)-Indexer源码分析
client-go之Indexer源码分析 1.Indexer概述 Indexer中有informer维护的指定资源对象的相对于etcd数据的一份本地内存缓存,可通过该缓存获取资源对象,以减少对api ...
- java标识符 identifier
1,标识符 --> 类名 方法名 变量名 常量名 接口名 为程序员自己命名的内容 main也是标识符但是不能修改 2, 命名规则 只能以 数字 字母(中文) 下划线 美元符号 ...
- 基于SqlSugar的开发框架循序渐进介绍(9)-- 结合Winform控件实现字段的权限控制
字段的权限控制,一般就是控制对应角色人员对某个业务对象的一些敏感字段的可访问性:包括可见.可编辑性等处理.本篇随笔结合基于SqlSugar的开发框架进行的字段控制管理介绍. 在设计字段权限的时候,我们 ...
- 关于vue项目中axios跨域的解决方法(开发环境)
1.在config文件中修改index.js proxyTable: { "/api":{ target: 'https://www.baidu.com/muc/',//你需要跨域 ...
- SAP APO-PP / DS
在SAP APO中,使用生产计划/详细计划(Production Planning/Detailed Scheduling)生成满足生产要求的采购建议. 此组件还用于定义资源计划和订单明细. 您还可以 ...
- Kali2019渗透环境配置
一.系统安装 二.基础配置 # 配置源 vim /etc/apt/sources.list # kali官方源 deb http://http.kali.org/ kali-rolling main ...
- Python实现12种概率分布(附代码)
今天给大家带来的这篇文章是关于机器学习的,机器学习有其独特的数学基础,我们用微积分来处理变化无限小的函数,并计算它们的变化:我们使用线性代数来处理计算过程:我们还用概率论与统计学建模不确定性. 在这其 ...
- python线程池 ThreadPoolExecutor 的用法及实战
写在前面的话 (https://jq.qq.com/?_wv=1027&k=rX9CWKg4) 文章来源于互联网从Python3.2开始,标准库为我们提供了 concurrent.future ...
- idea启动java Maven项目,出现" java: 程序包xxxx不存在"
解决办法如下:将idea的构建和运行托管到maven下面