POJ——T 1160 Post Office
http://poj.org/problem?id=1160
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 20218 | Accepted: 10899 |
Description
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
Output
Sample Input
- 10 5
- 1 2 3 6 7 9 11 22 44 50
Sample Output
- 9
Source
- #include <cstring>
- #include <cstdio>
- inline void read(int &x)
- {
- x=; register char ch=getchar();
- for(; ch>''||ch<''; ) ch=getchar();
- for(; ch>=''&&ch<=''; ch=getchar()) x=x*+ch-'';
- }
- const int N();
- #define min(a,b) (a<b?a:b)
- int n,m,x[N],minx[N][N],f[N][N];
- int Presist()
- {
- // freopen("post.in","r",stdin);
- // freopen("post.out","w",stdout);
- read(n),read(m);
- if(m==n) { puts(""); return ; }
- for(int i=; i<=n; ++i) read(x[i]);
- for(int i=; i<=n; ++i)
- for(int j=i+; j<=n; ++j)
- minx[i][j]=minx[i][j-]+x[j]-x[i+j>>];
- memset(f,/,sizeof(f));
- for(int i=; i<=n; ++i)
- f[i][]=minx[][i],f[i][i]=;
- for(int k=; k<=m; ++k)
- for(int i=; i<=n; ++i)
- {
- for(int j=i-; j; --j)
- f[i][k]=min(f[i][k],f[j][k-]+minx[j+][i]);
- }
- printf("%d\n",f[n][m]);
- return ;
- }
- int Aptal=Presist();
- int main(){;}
POJ——T 1160 Post Office的更多相关文章
- 【POJ】1160 Post Office
http://poj.org/problem?id=1160 题意:直线上有n个城市,其中有p个城市有邮局,问如何建p个邮局使得每个城市到最近的邮局和最小.(n<=300, p<=30&a ...
- POJ 1160 Post Office(区间DP)
Description There is a straight highway with villages alongside the highway. The highway is represen ...
- POJ 1160 Post Office(DP+经典预处理)
题目链接:http://poj.org/problem?id=1160 题目大意:在v个村庄中建立p个邮局,求所有村庄到它最近的邮局的距离和,村庄在一条直线上,邮局建在村庄上. 解题思路:设dp[i] ...
- POJ 1160 Post Office (动态规划)
Post Office Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 15412 Accepted: 8351 Desc ...
- poj 1160 Post Office (间隔DP)
Post Office Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 15966 Accepted: 8671 Desc ...
- [IOI 2000]POJ 1160 Post Office
Post Office Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22278 Accepted: 12034 Descrip ...
- POJ 1160 Post Office (四边形不等式优化DP)
题意: 给出m个村庄及其距离,给出n个邮局,要求怎么建n个邮局使代价最小. 析:一般的状态方程很容易写出,dp[i][j] = min{dp[i-1][k] + w[k+1][j]},表示前 j 个村 ...
- POJ 1160 Post Office
题意:有n个村庄,要在其中m个村庄里建邮局,每个村庄去邮局的代价为当前村庄到最近的一个有邮局村庄的路程,问总最小代价是多少. 解法:dp.dp[i][j]表示在前j个村庄建立i个邮局后的代价,则状态转 ...
- poj 1160 Post Office 【区间dp】
<题目链接> 转载于:>>> 题目大意: 一条高速公路,有N个村庄,每个村庄均有一个唯一的坐标,选择P个村庄建邮局,问怎么选择,才能使每个村庄到其最近邮局的距离和最小?最 ...
随机推荐
- akka设计模式系列-Backend模式
上一节我们介绍了Akka使用的基本模式,简单点来说就是,发消息给actor,处理结束后返回消息.但这种模式有个缺陷,就是一旦某个消息处理的比较慢,就会阻塞后面所有消息的处理.那么有没有方法规避这种阻塞 ...
- redis+php实现秒杀
使用redis队列,因为pop操作是原子的,即使有很多用户同时到达,也是依次执行,推荐使用(mysql事务在高并发下性能下降很厉害,文件锁的方式也是) 先将商品库存如队列 1 2 3 4 5 6 7 ...
- 巴什博弈----hdu2147-----较难
kiki's game Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 40000/10000 K (Java/Others)Total ...
- Java中static方法
今天学习到了并且应用到了java中的静态方法,并且了解到它的好处与缺点. ● 生命周期(Lifecycle): 静态方法(Static Method)与静态成员变量一样,属于类本身,在类装载的时候被装 ...
- 题解报告:hdu 2066 一个人的旅行
Problem Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰 ...
- ios cocos2d-x 多点触摸
需要在 AppController.mm文件中添加一句多点触摸支持: [_view setMultipleTouchEnabled:true];
- Eclipse中搭建Apache Tomcat7源码调试环境
第一步:获取Apache Tomcat7源码,读者可以从Apache 官方网站获取,官方下载地址: http://tomcat.apache.org/download-70.cgi 注意选择Sourc ...
- Contact
UF3000: 1.wafer进去prober后,默认probercard不会跟chuck上的wafer接触. 2.通过prober界面上的按钮向上移动,使得prober card和wafer的距离为 ...
- Andorid Scrolling Activity(CoordinatorLayout详情)
1.new project -> Scrolling Activity 2.Layout xml code activity_scrolling.xml <?xml version=&qu ...
- IC验证概念总结
一份代码,在写完之后,一定要再经过一次或多次整理和打磨,才能算完成的:一份代码,一定要把其有效代码行,精简.锤炼到最少.最短.最有效,才能算完成的. 下面这些问题和回答是忘记是在哪里看到的了,参考 ...