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. C语言 - 基础数据结构和算法 - 单向链表

    听黑马程序员教程<基础数据结构和算法 (C版本)>,照着老师所讲抄的, 视频地址https://www.bilibili.com/video/BV1vE411f7Jh?p=1 喜欢的朋友可 ...

  2. Spring框架 - Spring和Spring框架组成

    Spring框架 - Spring和Spring框架组成 Spring是什么?它是怎么诞生的?有哪些主要的组件和核心功能呢? 本文通过这几个问题帮助你构筑Spring和Spring Framework ...

  3. 关于个人全栈项目【臻美IT】博客类出现的问题以及解决方法

    每做一个项目,要记得写下心得哦,别偷懒啊!先上网址:https://www.maomin.club/ 这个项目属于博客类的,因为百度审核的问题就大体做了下,就当来练练手,里面文章链接的是CSDN的博客 ...

  4. RPA 快手自动上传机器人

    1.打开账号Cookie预存表格 2.机器人自动登录账号 3.机器人开始按照预设视频位置开始自动上传视频 4.机器人开始自动填写视频相关信息内容 5.完成后,可自动切换下一个账号继续上传

  5. while循环&&连接的两个条件

    做题的时候出现这种情况,把while中用&&连接的两个条件交换一下就会报错. 原因是 while 中是先检查第一个条件,如果第一个就为false就不看下一个了.如果第一个是true再检 ...

  6. SpringBoot接口 - 如何优雅的对参数进行校验?

    在以SpringBoot开发Restful接口时, 对于接口的查询参数后台也是要进行校验的,同时还需要给出校验的返回信息放到上文我们统一封装的结构中.那么如何优雅的进行参数的统一校验呢? @pdai ...

  7. 索尼笔记本Linux系统唤醒后,键盘无法使用

    1.编辑grub文件 sudo gedit /etc/default/grub 2.修改成以下参数 GRUB_CMDLINE_LINUX_DEFAULT="quiet splash i804 ...

  8. mvc Ensure that HttpConfiguration.EnsureInitialized()

    The object has not yet been initialized. Ensure that HttpConfiguration.EnsureInitialized() is called ...

  9. 4-12 Spring Security + JWT

    Spring Security + JWT 此前,在处理登录的业务中,当视为登录成功时,返回的字符串并不是JWT数据,则应该将此数据改为必要的JWT数据. @Service public class ...

  10. PostgreSQL下的SQL Shell(psql)工具

    首发微信公众号:SQL数据库运维 原文链接:https://mp.weixin.qq.com/s?__biz=MzI1NTQyNzg3MQ==&mid=2247485130&idx=1 ...