# OI——Post Office（OJ 162）

## 描述

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.

## 输入

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.

## 输出

The first line contains one integer S, which is the sum of all distances between each village and its nearest post office.

## 样例输入

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

9

## 题解（大部分借鉴）

1. 考虑在V个村庄中只建立一个邮局的情况，显然可以知道，将邮局建立在中间的那个村庄即可。也就是在a到b间建立一个邮局，若使消耗最小，则应该将邮局建立在（a+b)/2这个村庄上。
2. 下面考虑建立多个邮局的问题，可以这样将该问题拆分为若干子问题，在前i个村庄中建立j个邮局的最短距离，是在前k个村庄中建立j-1个邮局的最短距离与 在k+1到第i个邮局建立一个邮局的最短距离的和。而建立一个邮局我们在上面已经求出。
3. 状态表示，由上面的讨论，可以开两个数组dp[i][j]:在前i个村庄中建立j个邮局的最小耗费sum[i][j]:在第i个村庄到第j个村庄中建立1个邮局的最小耗费那么就有转移方程：dp[i][j]=min(dp[i][j],dp[k][j-1]+sum[k+1][i]);所要求的结果即为dp[V][P];
4. 然后就说说求sum数组的优化问题，可以假定有6个村庄，村庄的坐标已知分别为p1,p2,p3,p4,p5,p6;那么，如果要求sum[1][4]的话邮局需要建立在2或者3处,放在2处的消耗为p4-p2+p3-p2+p2-p1=p4-p2+p3-p1放在3处的结果为p4-p3+p3-p2+p3-p1=p4+p3-p2-p1，可见，将邮局建在2处或3处是一样的。现在接着求sum[1][5],现在处于中点的村庄是3，那么1-4到3的距离和刚才已经求出了，即为sum[1][4],所以只需再加上5到3的距离即可。同样，求sum[1][6]的时候也可以用sum[1][5]加上6到中点的距离。所以有递推关系：sum[i][j] = sum[i][j-1] + p[j] -p[(i+j)/2]

## 代码

 Source code
```#include <cstdio>
#include <cmath>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
#include <vector>
using namespace std;
int main(){
int m,n;
scanf ("%d%d",&m,&n);
int a[m + 1],sum[m + 1][m + 1],dp[m + 1][n + 1];
for (int i = 1; i <= m; i++){
scanf ("%d",&a[i]);
}
memset (sum,0,sizeof(sum));
//memset (dp,99999999,sizeof(dp));
for (int i = 1; i <= m; i++){
for (int j = i + 1; j <= m; j++){
sum[i][j] = sum[i][j - 1] + a[j] - a[(i + j) / 2];
}
}
for (int i = 1; i <= m; i++){
dp[i][1] = sum[1][i];
for (int j = 2; j <= n; j++){
dp[i][j] = 9999999;
}
}
for (int i = 1; i <= m; i++){
//dp[i][1] = sum[1][i];
for (int j = 2; j <= n; j++){
for (int k = 1; k <= i; k++){
dp[i][j] = min(dp[i][j],dp[k][j - 1] + sum[k + 1][i]);
//if (dp[i][1] == sum[1][i]){
//printf ("%d",dp[m][n]);
//return 0;
//}
}
}
}
printf ("%d\n",dp[m][n]);
return 0;
}
/*
10 5
1 2 3 6 7 9 11 22 44 50
*/```