poj1160 Post Office
2017-10-24 15:05
435 查看
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
题目大意:
给你一个递增的数组,代表每一个居民居住的位置,让你分配p个邮局地址使得所有居民到离他最近的邮局的距离总和最小,注意邮局可以盖在居民楼的位置。
[b]当只有一个邮局的时候,可以邮局应该放在从左到右第v/2的居民楼上的答案是最优的[/b]
[b]所以我们令f[i][j]表示前i个村庄,j个邮局的最小值[/b]
[b]直接枚举左端点,将邮局放在中间[/b]
[b]放邮局的代价用n^2直接预处理[/b]
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
题目大意:
给你一个递增的数组,代表每一个居民居住的位置,让你分配p个邮局地址使得所有居民到离他最近的邮局的距离总和最小,注意邮局可以盖在居民楼的位置。
题目思路:
dp想法。
当只有一个邮局的时候,可以邮局应该放在从左到右第v/2的居民楼上的答案是最优的。当有v个邮局的时候,在前v个村庄中建立j个邮局的最短距离,是在前
i个村庄中建立j-1个邮局的最短距离与在j+1到第i个邮局建立一个邮局的最短距离的和。建立一个邮局的答案可以预先处理。
附上代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
顶0踩0
上一篇动规:Investment
下一篇【dp】Super Jumping! Jumping! Jumping!
相关文章推荐
• POJ 1160:Post Office 邮局经典DP
• 深度学习部署系统构建--刘文志
• DP 【POJ1160】POST OFFICE 邮局问题
• 搜狗机器翻译技术分享--陈伟
• 邮局选址问题
• Hadoop生态系统零基础入门
• 南邮 OJ 1208 邮局选址问题
• 最懂程序员的学习方式 TensorFlow入门
• 邮局选址问题
• Retrofit 从入门封装到源码解析
• 二维带权邮局位置(选址)问题(分别求横坐标、纵坐标的带权中位数)C++实现
• 程序员如何转型AI工程师--蒋涛
• HZNUOJ 1804 邮局选址问题
• 邮局选址问题
• 【POJ1160】Post Office(动态规划 DP)
• dp四边形优化 poj 1160 Post Office题解
查看评论
暂无评论
您还没有登录,请[登录]或[注册]
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
个人资料
只为你而奋斗
访问:3259次
积分:112
等级:
排名:千里之外
原创:8篇
转载:0篇
译文:0篇
评论:1条
文章搜索
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
题目大意:
给你一个递增的数组,代表每一个居民居住的位置,让你分配p个邮局地址使得所有居民到离他最近的邮局的距离总和最小,注意邮局可以盖在居民楼的位置。
[b]当只有一个邮局的时候,可以邮局应该放在从左到右第v/2的居民楼上的答案是最优的[/b]
[b]所以我们令f[i][j]表示前i个村庄,j个邮局的最小值[/b]
[b]直接枚举左端点,将邮局放在中间[/b]
[b]放邮局的代价用n^2直接预处理[/b]
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; typedef long long lol; lol f[305][305],x[305],inf,c[305][305],ans; int n,m; int main() {int i,j,k; cin>>n>>m; for (i=1;i<=n;i++) scanf("%lld",&x[i]); memset(f,127/3,sizeof(f)); inf=f[0][0]; f[0][0]=0; for (i=1;i<=n;i++) { for (j=1;j<=i;j++) { int pos=(i+j)/2; for (k=j;k<=i;k++) { c[i][j]+=abs(x[pos]-x[k]); } } } for (i=1;i<=n;i++) { for (j=1;j<=m;j++) { for (k=1;k<=i;k++) { f[i][j]=min(f[i][j],f[k-1][j-1]+c[i][k]); } } } ans=inf; for (i=1;i<=m;i++) ans=min(ans,f [i]); cout<<ans; }
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
题目大意:
给你一个递增的数组,代表每一个居民居住的位置,让你分配p个邮局地址使得所有居民到离他最近的邮局的距离总和最小,注意邮局可以盖在居民楼的位置。
题目思路:
dp想法。
当只有一个邮局的时候,可以邮局应该放在从左到右第v/2的居民楼上的答案是最优的。当有v个邮局的时候,在前v个村庄中建立j个邮局的最短距离,是在前
i个村庄中建立j-1个邮局的最短距离与在j+1到第i个邮局建立一个邮局的最短距离的和。建立一个邮局的答案可以预先处理。
附上代码:
#include<iostream> #include<cstring> #include<cstdio> #include<cmath> using namespace std; int main(){ int v, p, a[1005]={0}, dp[305][50], sum[305][305]; cin >> v >> p; memset( sum, 0, sizeof(sum) ); for( int i=0 ; i<v ; i++ ){ for( int j=0 ; j<=p ; j++ ) dp[i][j] = 1<<29; } for( int i=0 ; i<v ; i++ ){ scanf( "%d", &a[i] ); } for( int i=0 ; i<v ; i++ ){ for( int j=i+1 ; j<v ; j++ ){ int pos = (i+j)/2; for( int k=i ; k<=j ; k++ ) sum[i][j] += abs( a[k]-a[pos] ); } } for( int i=0 ; i<v ; i++ ) dp[i][1] = sum[0][i]; for( int i=0 ; i<v ; i++ ){ for( int j=2 ; j<=p ; j++ ){ for( int k=0 ; k<=i ; k++ ){ dp[i][j] = min( dp[k][j-1]+sum[k+1][i], dp[i][j] ); } } } cout << dp[v-1][p] << endl; return 0; }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
顶0踩0
上一篇动规:Investment
下一篇【dp】Super Jumping! Jumping! Jumping!
相关文章推荐
• POJ 1160:Post Office 邮局经典DP
• 深度学习部署系统构建--刘文志
• DP 【POJ1160】POST OFFICE 邮局问题
• 搜狗机器翻译技术分享--陈伟
• 邮局选址问题
• Hadoop生态系统零基础入门
• 南邮 OJ 1208 邮局选址问题
• 最懂程序员的学习方式 TensorFlow入门
• 邮局选址问题
• Retrofit 从入门封装到源码解析
• 二维带权邮局位置(选址)问题(分别求横坐标、纵坐标的带权中位数)C++实现
• 程序员如何转型AI工程师--蒋涛
• HZNUOJ 1804 邮局选址问题
• 邮局选址问题
• 【POJ1160】Post Office(动态规划 DP)
• dp四边形优化 poj 1160 Post Office题解
查看评论
暂无评论
您还没有登录,请[登录]或[注册]
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
个人资料
只为你而奋斗
访问:3259次
积分:112
等级:
排名:千里之外
原创:8篇
转载:0篇
译文:0篇
评论:1条
文章搜索
相关文章推荐
- POJ 1160 Post Office
- poj1160 [IOI2000] Post Office(区间dp+四边形不等式)
- POJ 1160 Post Office
- poj 1160 Post Office 四边形优化
- POJ 1160Post Office
- POJ 1160 Post Office
- poj 1160 Post Office
- poj 1160 Post Office (区间DP)
- POJ 1160 Post Office
- 【DP】 POJ 1160 Post Office
- POJ1160 Post Office (四边形不等式优化DP)
- 【POJ 1160】Post Office
- poj1160 Post Office
- POJ1160--Post Office(四边形不等式优化)
- POJ——T 1160 Post Office
- poj-1160 post office (dp)
- [DP优化] POJ 1160 Post Office
- POJ 1160 Post Office
- 【POJ】1160 Post Office
- poj 1160 post office