Post Office

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

 

在 J 市的一条笔直的公路旁分布着 n 个村庄,每个村庄都有一个唯一的坐标 Xi,任意一
对村庄的坐标不同。最近,J 市领导计划在村庄里新建 m 个邮局,而邮局在 n 个村庄里
的分布情况会影响到居民的便利程度。
设 m 个邮局分别建在 P1,P2,..,Pm 号村庄。每个村庄的村民都会找到与其距离最近的一
个邮局, 若有多个距离最近的则会任选一个, 该村庄的便利度即为该村庄与其最近的邮局的
距离,而所有村庄的便利度的和即为总便利度。

 

含义:

dp[i][j] 代表前i个村庄放了j个邮局的最小便利度

sum[i][j]代表从第i个村庄到第j个村庄放一个有据的最小便利度

转移 :

dp[i][j] =min (dp[i][j],d[k][j-1]+sum[k+1][i])  (j-1<=k<=i);

sum数组的求法 :

假设 有三个村庄 p1,p2,p3 那邮局就放在p2处  

若有第四个村庄 邮局就放在p2 或p3处 

若有第五个村庄 邮局放在 p3处 

若有第六个村庄 邮局放在 p3 或 p4处 

也许你看出规律来了 sum[i][j]可以由 sum[i][j-1] + a[i]-a[(i+j)>>1] 递推而来 

 

 1 #include <cstdio>
 2 #include <cctype>
 3 
 4 const int INF=0x3f3f3f3f;
 5 const int MAXN=310;
 6 
 7 int n,m;
 8 
 9 int a[MAXN],dp[MAXN][MAXN],sum[MAXN][MAXN];
10 
11 inline void read(int&x) {
12     int f=1;register char c=getchar();
13     for(x=0;!isdigit(c);c=='-'&&(f=-1),c=getchar());
14     for(;isdigit(c);x=x*10+c-48,c=getchar());
15     x=x*f;
16 }
17 
18 inline int min(int a,int b) {return a<b?a:b;}
19 
20 int hh() {
21     freopen("post.in","r",stdin);
22     freopen("post.out","w",stdout);
23     read(n);read(m);
24     for(int i=1;i<=n;++i) read(a[i]);
25     for(int i=1;i<=n;++i)
26       for(int j=i+1;j<=n;++j)
27         sum[i][j]=sum[i][j-1]+a[j]-a[(i+j)>>1];
28     for(int i=1;i<=n;++i) dp[i][1]=sum[1][i];
29     for(int j=2;j<=m;++j)
30       for(int i=j+1;i<=n;++i) {
31           dp[i][j]=INF;
32           for(int k=j-1;k<i;++k)
33             dp[i][j]=min(dp[i][j],dp[k][j-1]+sum[k+1][i]);
34       }        
35     printf("%d\n",dp[n][m]);
36     fclose(stdin);
37     fclose(stdout);
38     return 0;
39 }
40 
41 int sb=hh();
42 int main(int argc,char**argv) {;}
代码

 

 

posted @ 2017-09-20 14:43  拿叉插猹哈  阅读(842)  评论(0编辑  收藏  举报