题解【POJ1160】Post Office
Description
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.
Output
The first line contains one integer S, which is the sum of all distances between each village and its nearest post office.
Sample Input
10 5
1 2 3 6 7 9 11 22 44 50
Sample Output
9
Source
Solution
简化版题意:有N个村庄,每个村庄均有一个唯一的坐标,选择P个村庄建邮局,问怎么选择,才能使每个村庄到其最近邮局的距离和最小,输出这个最小值。
本题是一道区间DP题,比较复杂。
当我们在v个村庄中只建一个邮局,可以推导出,只有邮局位于中间位置,距离和才最小。
有一个特殊情况是,当村庄数为偶数,中间位置有两个村庄,经过计算,两个村庄的距离和相等,所以两个位置均可。
可以联想到,N个村庄建P个邮局,相当于每个邮局均有一个作用范围,该邮局位于其作用范围的中间位置,就是要找到一个k,使前k个村庄建P - 1个邮局,最后几个村庄建一个邮局的方案满足题意。
那么,我们设:
dp[i][j]:前i个村庄建j个邮局的最小距离和
b[i][j]:第i个村庄到第j个村庄之间建1个邮局的最小距离和
因此,状态转移方程就是:
dp[i][j] = min(dp[i][j],dp[k][j - 1] + b[k + 1][j])
还有一点,计算b[i][j]时,b[i][j - 1]已经计算出来,而且可以推导出无论j为奇数还是偶数,b[i][j]均可以写成b[i][j - 1] + j距离i、j中点的距离。
Code
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>//头文件
using namespace std;//使用标准名字空间
inline int gi()//快速读入
{
int f = 1, x = 0;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
{
x = x * 10 + c - '0';
c = getchar();
}
return f * x;
}
int n, m, a[305], sum, b[305][305], dp[305][305];//m即为题中的p,sum为最终答案,b数组和dp数组的含义同Solution
int main()
{
n = gi(), m = gi();
for (int i = 1; i <= n; i++)
{
a[i] = gi();
}
//输入
memset(dp, 0x3f3f3f3f, sizeof(dp));//初始化dp数组为最大值
for (int i = 1; i < n; i++)
{
for (int j = i + 1; j <= n; j++)
{
b[i][j] = b[i][j - 1] + a[j] - a[(i + j) >> 1];//b数组的初始化
}
}
for (int i = 1; i <= n; i++)
{
dp[i][1] = b[1][i];//只建一个邮局的预处理
}
for (int i = 2; i <= m; i++)//要建i个邮局
{
for (int j = i; j <= n; j++)//1~j号村庄建i个邮局
{
for (int k = i - 1; k <= j - 1; k++)//1~k号村庄建i- 1个邮局
{
dp[j][i] = min(dp[j][i], dp[k][i - 1] + b[k + 1][j]);//DP主过程
}
}
}
sum = dp[n][m];//答案即为dp[n][m],就是在1~n号村庄中建m个邮局
printf("%d", sum);//输出最终答案
return 0;//结束
}
题解【POJ1160】Post Office的更多相关文章
- POJ1160 Post Office[序列DP]
Post Office Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 18680 Accepted: 10075 Des ...
- POJ1160 Post Office (四边形不等式优化DP)
There is a straight highway with villages alongside the highway. The highway is represented as an in ...
- poj1160 post office
题目大意:有n个乡村,现在要建立m个邮局,邮局只能建在乡村里.现在要使每个乡村到离它最近的邮局距离的总和尽量小,求这个最小距离和. n<300,p<30,乡村的位置不超过10000. 分析 ...
- POJ-1160 Post Office (DP+四边形不等式优化)
题目大意:有v个村庄成直线排列,要建设p个邮局,为了使每一个村庄到离它最近的邮局的距离之和最小,应该怎样分配邮局的建设,输出最小距离和. 题目分析:定义状态dp(i,j)表示建设 i 个邮局最远覆盖到 ...
- [IOI2000][POJ1160]Post office
题面在这里 题意 一条路上有\(n\)个村庄,坐标分别为\(x[i]\),你需要在村庄上建设\(m\)个邮局,使得 每个村庄和最近的邮局之间的所有距离总和最小,求这个最小值. 数据范围 \(1\le ...
- [POJ1160] Post Office [四边形不等式dp]
题面: 传送门 思路: dp方程实际上很好想 设$dp\left[i\right]\left[j\right]$表示前$j$个镇子设立$i$个邮局的最小花费 然后状态转移: $dp\left[i\ri ...
- DP---基本思想 具体实现 经典题目 POJ1160 POJ1037
POJ1160, post office.动态规划的经典题目.呃,又是经典题目,DP部分的经典题目怎就这么多.木有办法,事实就这样. 求:在村庄内建邮局,要使村庄到邮局的距离和最小. 设有m个村庄,分 ...
- HDU3480 Division——四边形不等式或斜率优化
题目大意 将N个数分成M部分,使每部分的最大值与最小值平方差的和最小. 思路 首先肯定要将数列排序,每部分一定是取连续的一段,于是就有了方程 $\Large f(i,j)=min(f(i-1,k-1) ...
- IOI2000 Post Office (POJ1160)
前言 昨天XY讲课!讲到这题!还是IOI的题!不过据说00年的时候DP还不流行. 题面 http://poj.org/problem?id=1160 分析 § 1 中位数 首先我们考虑,若有x1 & ...
随机推荐
- #4864. [BeiJing 2017 Wc]神秘物质 [FHQ Treap]
这题其实挺简单的,有个东西可能稍微难维护了一点点.. \(merge\ x\ e\) 当前第 \(x\) 个原子和第 \(x+1\) 个原子合并,得到能量为 \(e\) 的新原子: \(insert\ ...
- 图片选择并使用base64展示
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 安装DHCP到CentOS(YUM)
运行环境 系统版本:CentOS Linux release 7.3.1611 (Core) 软件版本:DHCP-x 硬件要求:无 安装过程 1.安装YUM源,由EPEL提供 [root@localh ...
- hdu 1257 最少拦截系统 (最长上升子序列/贪心)
题意:某国为了防御敌国的导弹袭击,发展出一种导弹拦截系统.但是这种导弹拦截系统有一个缺陷:虽然它的第一发炮弹能够到达任意的高度,但是以后每一发炮弹都不能超过前一发的高度.某天,雷达捕捉到敌国的导弹来袭 ...
- jvm gc 调优 实战
非常不错的文章们 转自: 中文:http://blog.csdn.net/dragonassassin/article/details/51010947 http://josh-persistence ...
- C++ const char *返回值问题
今天写代码,遇到一个const char *返回值的问题,记录一下 问题场景:我写了一个动态库,有个函数声明如下: ; 函数定义如下: const char * HttpRequestImpl::RG ...
- 2020牛客寒假算法基础集训营6 C 汉诺塔 (dp 最长下降子序列)
https://ac.nowcoder.com/acm/contest/3007/C 将木板按照Xi从小到大排序,将这时的Yi数列记为Zi数列,则问题变成将Zi划分为尽可能少的若干组上升子序列. 根据 ...
- yii2 环境切换(开发,正式)
方式一,在web中index修改 开发环境配置 web目录index.php defined('YII_DEBUG') or define('YII_DEBUG', true); defined('Y ...
- R语言函数化编程笔记1
R语言函数化编程笔记1 notes:有一个不错的网站叫做stack overflow,有问题可以从上面找或者搜索答案,会有大佬相助. 在github上面可以找到很多R的扩展包,如果自己额修改被接受,那 ...
- LCP 2-分式化简
LCP 2-分式化简 public int[] fraction(int[] cont) { int len = cont.length; int[] d = new int[]{cont[len - ...