POJ 1160 Post Office

题目

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

InputcopyOutputcopy
10 5
1 2 3 6 7 9 11 22 44 50
9

题目大意

有一条笔直的公路,公路旁有村庄。公路用整数轴表示,每个村庄的位置用单个整数坐标标识。没有两个村庄处于同一位置。两个位置之间的距离是它们的整数坐标差的绝对值。

邮局将建在一些村庄,但不一定是所有的村庄。一个村庄和里面的邮局有相同的位置。为了修建邮局,应选择其位置,使每个村庄与其最近的邮局之间的所有距离总和最小。

你要写一个程序,根据村庄的位置和邮局的数量,计算每个村庄和最近邮局之间所有距离的最小总和。

分析

        我们先来思考给定区间中间一个邮局怎样做优

        当只有一个村庄时,显然建在该村庄上是最优

        当有两个村庄时,将在这两个村庄上是一样优的,都为两村之间距离

        当有三个村庄时,建在中间村庄上时最优,设三点坐标为 x , y , z

                当在 x 上,sum1=(z-x)+(y-x)=y+z-2*x;

                当在 y 上,sum2=(y-x)+(z-y)=z-x;

                当在 z 上,sum3=(z-x)+(z-y)=2*z-x-y;

                  易证,sum2最小

        那么扩展到区间,在最中间地方建一定是最短的

 g[i][j] 表示村庄i到村庄j建立一个邮局的距离,那么g[i][j]=g[i][j-1]+a[j]-a[(i+j)/2];

g[1][4](建在2或者3)=(a[4]-a[3])+(a[3]-a[2])+(a[3]-a[1])  =a[4]+a[3]-a[2]-a[1]

而g[1][5](建在3)=(a[5]-a[3])+(a[4]-a[3])+(a[3]-a[2])+(a[3]-a[1])  =a[5]+a[4]-a[2]-a[1]

通过这样可以发现这个规律:g[i][j]=g[i][j-1]+a[j]-a[(i+j)/2]

所以这样就把建在1到 n 个村庄的 p 个邮局这个问题拆开了,知道了某部分建立一个邮局的最优解,之后就是区间dp推导。

用 f [i][j]表示从1到j个村庄建立第i个邮局时的最优解,则有f[i][j]=min(f[i][j],f[k][j-1]+g[k+1][i])[j]);

关于初值,借鉴了一下别的题解,就是 f[i][1]=g[1][i],即为让1~ i 区间内建1个

Code

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
//POJ不能用万能头,这几个是借鉴来的
using namespace std;
int n,p,a[305],g[305][305],f[305][305];
int main()
{
 	cin>>n>>p;
 	for(int i=1;i<=n;i++) cin>>a[i];
 	for(int i=1;i<=n;i++)
 		for(int j=i+1;j<=n;j++)
 			g[i][j]=g[i][j-1]+a[j]-a[(i+j)/2];// 除以2是向下取整
	for(int i=2;i<=n;i++) f[i][1]=g[1][i];//初始化
	for(int j=2;j<=p;j++)
		for(int i=1;i<=n;i++)
		{
			f[i][j]=1e9;
			for(int k=j-1;k<i;k++) f[i][j]=min(f[i][j],f[k][j-1]+g[k+1][i]);
		}
	cout<<f[n][p];
	return 0;
}

  • 37
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值