Post Office
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 15256 | Accepted: 8264 |
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.
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个村子,然后在这v个村子里面建立p个邮局,给出v个村子的坐标,求v个村子到最近的邮局的距离的和。
思路是这样的,dp[ i ][ j ]表示前J个村子建立I个邮局的最小距离,那么在此基础上,如果在 j+1 到 j+k 个村子中再建立一个邮局,最小距离和就是
dp[ i+1 ][ j+k ]= max(dp[ i ][ j ] + cost[j+1][j+k],dp[i+1][j+k]) ;
上面的 cost[ a ][ b ] 表示 在村庄 a 到 b 中建立 1 个邮局的最小距离和。显然,这个邮局应该建在(a+b)/2这个村庄位置上。
初始化:
dp为一个最大值 并且dp[1][j]=cost[1][j]; 在前j个村子中只建立一个邮局
AC代码:
#include<iostream> #include<algorithm> #include<cstring> #include<math.h> using namespace std; #define N 305 int cost[N][N],dp[N][N]; int a[N]; int main() { int v,p,n,m,i,j,k; memset(cost,0,sizeof(cost)); while(cin>>v>>p) { for(i=1;i<=v;++i) cin>>a[i]; for(i=1;i<=v;++i) for(j=i;j<=v;++j) { int mid=(i+j)/2; for(k=i;k<=j;++k) cost[i][j]+=abs(a[k]-a[mid]); } /*cout<<endl; for(i=1;i<=v;++i) { for(j=1;j<=v;++j) cout<<cost[i][j]<<" "; cout<<endl; }*/ memset(dp,125,sizeof(dp)); for(i=1;i<=v;++i) dp[1][i]=cost[1][i]; for(i=1;i+1<=p;++i) for(j=1;j<=v;++j) for(k=0;k+j<=v;++k) dp[i+1][j+k]=min(dp[i][j]+cost[j+1][j+k],dp[i+1][j+k]); cout<<dp[p][v]<<endl; /*for(i=1;i<=p;++i) { for(j=1;j<=v;++j) cout<<dp[i][j]<<" "; cout<<endl; }*/ } }