Post Office
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 14051 |
|
Accepted: 7576 |
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
IOI 2000
分析一下,这题,很不错的dp题,首先,dp[i][j]表示,从0到i修j个post office,sum[i][j]表示从i到j修一个post office的最短路!
那么dp[i][j]=dp[k][j-1]+sum[k+1][j](k>=0 k<=i);
sum[i][j]=sum[i][j-1]+p[i]-p[(i+j)/2];
我们讲讲这两个式子的得来!
首先,我们可以得到一个结论,在偶数个点中建一个邮局的最短方法,就是在,中间那两个点,且这两个点的距离是等价的!
如p0,p1,p2,p3,我们在p1,p2建在哪都是一个,都是p3-p1+p2-p0,这一点可以推广到所有的偶数个点,奇数个点,自然也是在最中间,
但是只有一个点!如么我们想要在加一个点呢?如在上p0,p1,p2,p3中加一个p4,那么怎么由sum[i][j-1]推到sum[i][j]呢?我们可以发现
只要加一个p4到p2的距离也就是p[j]-p[(i+j)/2]就可以了,其实,这个例子是很简单的,但也足也推到一般情况,我们再想,如果是
奇数个点呢?就比如p0,p1,p2,p3,p4再加上一个p5,最短路怎么算呢?我们可以很明显知道也是加上p[j]-p[(i+j)/2]!
我们再说说第一个式子的得来,
我们可以知道,从0到i,放j个邮局的最短方法,一定是0到i的某一个点k,得来的,也就是dp[k][j-1]得来的,因为,我们可以把k从0到
i进行,全部枚举,这样,前k个点,放j个邮局的最短路,加上一个从k+1到j放一个邮局的最短路,就可以得到了!这样这题就解决了,但是在极端的时候,dp[i][0]=sum[0][i],这了是要初始化做的,这题就可以a了!
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
#define inf 0x4f4f4f4f
int dp[320][320],sum[320][320],p[320];
int fmin(int a,int b)
{
if(a<b)
return a;
return b;
}
int main()
{
int n,m,i,j,k;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0;i<n;i++)
{
scanf("%d",&p[i]);
}
memset(sum,0,sizeof(sum));
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
{
sum[i][j]=sum[i][j-1]+p[j]-p[(i+j)/2];
}
for(i=0;i<n;i++)
{
dp[i][0]=sum[0][i];
for(j=1;j<m;j++)
{
dp[i][j]=inf;
}
}
for(i=0;i<n;i++)
for(j=1;j<m;j++)
for(k=0;k<i;k++)
{
dp[i][j]=fmin(dp[i][j],dp[k][j-1]+sum[k+1][i]);
}
printf("%d\n",dp[n-1][m-1]);//计数,都是从0开始的
}
return 0;
}
再来一个四边形优化的dp
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
#define inf 0x4f4f4f4f
int dp[320][320],sum[320][320],p[320],kk[320][320];
int fmin(int a,int b)
{
if(a<b)
return a;
return b;
}
int main()
{
int n,m,i,j,k;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=1;i<=n;i++)
{
scanf("%d",&p[i]);
}
memset(sum,0,sizeof(sum));
for(i=1;i<=n;i++)
{
sum[i][i]=0;
for(j=i+1;j<=n;j++)
{
sum[i][j]=sum[i][j-1]+p[j]-p[(i+j)/2];
}
}
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
dp[i][j]=inf;
}
}
for(i=1;i<=n;i++)
{
kk[1][i]=0;
dp[1][i]=sum[1][i];
}
for(i=2;i<=m;i++)
{
kk[i][n+1]=n;
for(j=n;j>i;j--)
{
for(k=kk[i-1][j];k<=kk[i][j+1];k++)
{
if(dp[i-1][k]+sum[k+1][j]<dp[i][j])
{
dp[i][j]=dp[i-1][k]+sum[k+1][j];
kk[i][j]=k;
}
}
}
}
printf("%d\n",dp[m][n]);
}
return 0;
}