poj 1160
2011-05-08 20:47
204 查看
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
Sample Output
Source
IOI 2000
分析:经典的邮局问题,直接没优化敲了一遍,110ms,好慢。。。
代码:
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 10498 | Accepted: 5649 |
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
分析:经典的邮局问题,直接没优化敲了一遍,110ms,好慢。。。
代码:
#include<cstdio> int i,j,k,v,p,x[301],g[301][301],f[300][31]; inline void max(int &a,int b) { if(a>b)a=b; } void get(int l,int r) { for(int i=l,m=(l+r)>>1;i<=r;++i) if(i<m)g[l][r]+=x[m]-x[i]; else if(i>m)g[l][r]+=x[i]-x[m]; } int main() { scanf("%d %d",&v,&p); for(i=1;i<=v;++i)scanf("%d",&x[i]); for(i=1;i<=v;++i) for(j=i+1;j<=v;++j)get(i,j); for(i=0;i<=v;++i) for(j=0;j<=p;++j)f[i][j]=0x7FFFFFF; f[0][0]=0; for(i=1;i<=v;++i) for(j=1;j<=p;++j) for(k=1;k<=i;++k) max(f[i][j],f[k-1][j-1]+g[k][i]); printf("%d/n",f[v][p]); }
相关文章推荐
- POJ 1160 Post Office
- poj 1160
- POJ1160 【Post Office】
- POJ 1160 Post Office
- Poj-1160 Post Office(经典dp
- poj 1160 post office(邮局)
- POJ 1160 Post Office
- POJ 1160 Post Office
- POJ 1160 Post Office
- ioi2000 poj 1160 VIJOS-P1242 邮局问题
- poj 1160Post Office
- poj1160 Post Office
- 【DP|数学+预处理】POJ-1160 Post Office
- poj 1160
- poj 1160 Post Office (间隔DP)
- Post Office POJ - 1160
- poj 1160
- poj1160 Post Office
- [dp] poj1160 Post office
- POJ-1160 Post Office (DP+四边形不等式优化)