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的更多相关文章

  1. BZOJXXXX: [IOI2000]邮局——四边形不等式优化初探

    貌似$BZOJ$上并没有这个题... 是嫌这个题水了么... 还是要氪金权限号??? 这里附上洛谷的题面:洛谷P4767 [IOI2000]邮局 题目描述 高速公路旁边有一些村庄.高速公路表示为整数轴 ...

  2. 题解——洛谷P4767 [IOI2000]邮局(区间DP)

    这题是一道区间DP 思维难度主要集中在如何预处理距离上 由生活经验得,邮局放在中间显然最优 所以我们可以递推求出\( w[i][j] \)表示i,j之间放一个邮局得距离 然后设出状态转移方程 设\( ...

  3. hdu 2829 Lawrence(四边形不等式优化dp)

    T. E. Lawrence was a controversial figure during World War I. He was a British officer who served in ...

  4. BZOJ1563/洛谷P1912 诗人小G 【四边形不等式优化dp】

    题目链接 洛谷P1912[原题,需输出方案] BZOJ1563[无SPJ,只需输出结果] 题解 四边形不等式 什么是四边形不等式? 一个定义域在整数上的函数\(val(i,j)\),满足对\(\for ...

  5. 【转】斜率优化DP和四边形不等式优化DP整理

    (自己的理解:首先考虑单调队列,不行时考虑斜率,再不行就考虑不等式什么的东西) 当dp的状态转移方程dp[i]的状态i需要从前面(0~i-1)个状态找出最优子决策做转移时 我们常常需要双重循环 (一重 ...

  6. codevs3002石子归并3(四边形不等式优化dp)

    3002 石子归并 3 参考 http://it.dgzx.net/drkt/oszt/zltk/yxlw/dongtai3.htm  时间限制: 1 s  空间限制: 256000 KB  题目等级 ...

  7. CF321E Ciel and Gondolas Wqs二分 四边形不等式优化dp 决策单调性

    LINK:CF321E Ciel and Gondolas 很少遇到这么有意思的题目了.虽然很套路.. 容易想到dp \(f_{i,j}\)表示前i段分了j段的最小值 转移需要维护一个\(cost(i ...

  8. HDU 2829 Lawrence (斜率优化DP或四边形不等式优化DP)

    题意:给定 n 个数,要你将其分成m + 1组,要求每组数必须是连续的而且要求得到的价值最小.一组数的价值定义为该组内任意两个数乘积之和,如果某组中仅有一个数,那么该组数的价值为0. 析:DP状态方程 ...

  9. 四边形不等式优化DP——石子合并问题 学习笔记

    好方啊马上就要区域赛了连DP都不会QAQ 毛子青<动态规划算法的优化技巧>论文里面提到了一类问题:石子合并. n堆石子.现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆石子合并成新的 ...

随机推荐

  1. 【lora无线数传通信模块】亿佰特E22串口模块用于物联网地震预警传感通信方案

    物联网地震预警项目介绍: 地震,俗称地动.它像平常的刮风下雨一样,是一种常见的自然现象,是地壳运动的一种表现,即地球内部缓慢积累的能量突然释放而引起的地球表层的振动.据统计,5级以上地震就能够造成破坏 ...

  2. mysql复制表的两种方式

    mysql复制表的两种方式. 第一.只复制表结构到新表 create table 新表 select * from 旧表 where 1=2 或者 create table 新表 like 旧表 第二 ...

  3. 一些好用的javascript/typescript方法封装分享

    1.数字格式化 JS版-直接写到原型链上 /** * @author: silencetea * @name: * @description: 数字格式化,默认每三位用英文逗号分隔 * @param ...

  4. JS:object

    object:对象 1.对象是拥有属性和方法的数据,也是一个变量,但值有多个,以key-value的形式. 2.对象有继承属性: 每当创建一个对象,对象里面就会有一个原型对象prototype,可以从 ...

  5. python基础知识-day8(动态参数)

    1.动态参数 函数的形式参数个数不确定.函数的形式数据类型不确定,使用动态参数,*代表元组,**代表字典. 2.代码案例演示 1 def func(*args,**kwargs): 2 print(a ...

  6. 记录一下MySql update会锁定哪些范围的数据

    目录 1.背景 2.前置知识 2.1 数据库的隔离级别 2.2 数据库版本 2.3 数据库的存储引擎 2.4 锁是加在记录上还是索引上 2.5 update...where加锁的基本单位是 2.6 行 ...

  7. jenkins自动触发构建

    1. 安装jenkins cat /etc/yum.repos.d/jenkins.repo [jenkins] name=Jenkins baseurl=http://pkg.jenkins.io/ ...

  8. VisonPro · 视觉定位工具包示例

    一.概述 视觉定位工具包一般包含: 1.相机取像: 2.图像九点标定: 3.Mark点粗定位: 4.建立粗定位坐标系: 5.Mark点精定位 6.输出Mark点坐标,角度等信息. 二.分类 1.单特征 ...

  9. 『现学现忘』Git后悔药 — 29、版本回退git reset --mixed命令说明

    git reset --mixed commit-id命令:回退到指定版本.(mixed:混合的,即:中等回退.) 该命令不仅修改了分支中HEAD指针的位置,还将暂存区中数据也回退到了指定版本. 但是 ...

  10. java发送http请求get/post

    1,导入依赖 <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId> ...