题目:​​http://poj.org/problem?id=1160​


Post Office


Time Limit: 1000MS

 

Memory Limit: 10000K

Total Submissions: 17378

 

Accepted: 9364


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


分析:

大意是,在v个地址升序的不同地点选择p个地点作为post的建立地点,求所有的地址距离他们最近的post的距离和。

做完这题我发现逻辑分析和公式推导能力真的very very very important.


感觉自己的动态规划一直没有学好,重申一下dp的核心:多阶段过程转化为一系列单阶段问题,利用各阶段之间的关系,逐个求解(多阶段-->单阶段).

本问题最简单的阶段:1,2,3,4,5,6,7中选一个地址作为邮局。那么选2个邮局呢?则是选定一个界,一段选一个邮局(转化成最简单的单阶段)。3邮局-->2邮局

……n邮局-->n-1邮局.

错误的推论:设界的坐标是k,startdex到k建立j-1个邮局的最佳距离是dp[k][j-1],k+1到enddex建立1个邮局的距离结果是sum[k+1]。那么就有递推式:dp[enddex][j]=min(dp[k][j-1]+sum[k+1],dp[enddex][j]); dp[enddex][j]与dp[k][j-1]根本就没有递推关系!

改正:start--q2建立j个邮局dp[q2][j]=dp[k][j-1]+sum[k+1][q2]; 问题结果就是dp[v][p].

关于sum数组的求解,确定上下界和界再求最小距离这循环数该是4吧。。。

优化:d1,d2的post分布最简单,d1,d2,d3的post选择一定是中间吗?这是一定的。

设选择的地址是di,那么距离和就是di-d1+|d2-di|+d3-di=d3-d1+|d2-di|.所以di=d2时最小。

当有偶数个post呢?d1,d2,d3,d4的相关距离就是d4-d1+|d2-di|+|d3-di|很明显,di=d2 or di=d3 最小且相同,sum4=d4-d1+d3-d2。

可以发现,所有的数字都满足这样的中间性质。能不能从4地址推到5地址?

d1,d2,d3,d4,d5的最佳距离应该是所有地址到d3的距离和,即d5-d1+d4-d2.也即是sum[1][4]+d5-d3

=d4-d1+d3-d2+d5-d3=d5-d1+d4-d2. 即sum[i][j]=sum[i][j-1]+p[j]-p[(j+i)/2].

接下来敲代码~~~


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
int p[305],sum[305][305],dp[305][35];
int main()
{
//freopen("cin.txt","r",stdin);
int v,pn;
while(cin>>v>>pn){
for(int i=1;i<=v;i++){
scanf("%d",&p[i]);
}
memset(sum,0,sizeof(sum));
sum[1][2]=p[2]-p[1];
for(int i=1;i<v;i++){
for(int j=i+1;j<=v;j++){
sum[i][j]=sum[i][j-1]+p[j]-p[(j+i)/2];
}
}
for(int i=1;i<=v;i++){
dp[i][1]=sum[1][i];
}
for(int j=2;j<=pn;j++){
for(int i=1;i<=v;i++){
for(int k=1;k<=i;k++) {
if(dp[i][j]==0)dp[i][j]=dp[k][j-1]+sum[k+1][i];
else dp[i][j]=min(dp[i][j],dp[k][j-1]+sum[k+1][i]);
}
}
}
printf("%d\n",dp[v][pn]);
}
return 0;
}