[IOI 2000]POJ 1160 Post Office
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 22278 | Accepted: 12034 |
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
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
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
【题意】
依次给定n个村庄在这条直线的位置,在n个村庄中建立p个邮局,求所有村庄到它最近的邮局的距离和,村庄在一条直线上,邮局建在村庄上。
【分析】
首先求出在连续的几个村庄上建立一个邮局的最短距离,用数组dis[i][j]表示在第i个村庄和第j个村庄之间建一个邮局的最短距。
dis[i][j]=dis[i][j-1]+x[j]-x[(i+j)/2]; (村庄位置为x[i])
用数组dp[i][j]表示在前i个村庄中建立j个邮局的最小距离。即在前k(k<i)个村庄建立j-1个邮局,在k+1到j个村庄建立一个邮局。
dp[i][j]=min(dp[i][j],dp[k][j-1]+dis[k+1][i])
【代码】
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int N=305;
int n,p,x[N],dis[N][N],f[N][35];
inline void Init(){
scanf("%d%d",&n,&p);
for(int i=1;i<=n;i++) scanf("%d",&x[i]);
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
dis[i][j]=dis[i][j-1]+x[j]-x[i+j>>1];
}
}
}
inline void Solve(){
memset(f,0x3f,sizeof f);
for(int i=1;i<=p;i++) f[i][i]=0;
for(int i=1;i<=n;i++) f[i][1]=dis[1][i];
for(int j=2;j<=p;j++){
for(int i=j+1;i<=n;i++){
for(int k=j-1;k<i;k++){
f[i][j]=min(f[i][j],f[k][j-1]+dis[k+1][i]);
}
}
}
printf("%d\n",f[n][p]);
}
int main(){
Init();
Solve();
return 0;
}
[IOI 2000]POJ 1160 Post Office的更多相关文章
- POJ 1160 Post Office (动态规划)
Post Office Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 15412 Accepted: 8351 Desc ...
- 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 (间隔DP)
Post Office Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 15966 Accepted: 8671 Desc ...
- 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个村庄建邮局,问怎么选择,才能使每个村庄到其最近邮局的距离和最小?最 ...
- POJ.1160.Post Office(DP 四边形不等式)
题目链接 \(Description\) 一条直线上有n个村庄,位置各不相同.选择p个村庄建邮局,求每个村庄到最近邮局的距离之和的最小值. \(Solution\) 先考虑在\([l,r]\)建一个邮 ...
- POJ——T 1160 Post Office
http://poj.org/problem?id=1160 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20218 ...
随机推荐
- Mui 下拉刷新,刷新完成功能实现
Mui中,正在刷新后,就直接回弹了,没有刷新完成这个过程,然后我就在中间添加了一个过程. 代码如下: //-----------日期格式化------------- function form ...
- Android 热修复方案Tinker(一) Application改造
基于Tinker V1.7.5 Android 热修复方案Tinker(一) Application改造 Android 热修复方案Tinker(二) 补丁加载流程 Android 热修复 ...
- Mybatis Dynamic Query 更新
文章目录 1. 简介 2. 准备工作 3. 开始更新 3.1. update 3.2. update Null 4. 结束 5. 关注@我 项目地址:https://github.com/wz2coo ...
- [Android Pro] Android P版本 新功能介绍和兼容性处理(三)Android Studio 3.0 ~ 3.2 其他特性
cp : https://blog.csdn.net/yi_master/article/details/80067198 1:JAVA8特性支持 1)Base64.java 在升级到as3.0之后, ...
- 解决微信小程序ios端滚动卡顿的问题
方案1:直接使用微信小程序提供的 “scroll-view " 组件. <scroll-view scroll-y style="height: 100%;"> ...
- 微信小程序- 提示不在以下合法域名列表中
第一次开发微信小程序时在访问后台数据时总是提示 提示上面问题主要有两个原因: 1.为配置安全合法域名列表: 微信小程序在开发时需要在官网配置固定的数据来源网站: 登录小程序平台中->设置: 图中 ...
- 按装 autoconf 出现 需要安装m4-1.4.16.tar.gz的错误
root@suzgc # /usr/local/php/bin/phpize ./configure --with-php-config=/usr/local/php/bin/php-config - ...
- CAP Twelve Years Later: How the "Rules" Have Changed
The CAP theorem asserts that any networked shared-data system can have only two of three desirable ...
- 转 全面理解Javascript闭包和闭包的几种写法及用途
转自:http://www.cnblogs.com/yunfeifei/p/4019504.html 好久没有写博客了,过了一个十一长假都变懒了,今天总算是恢复状态了.好了,进入正题,今天来说一说ja ...
- python下申明式的对象关系DB映射器--Pony
之前看到了Sails.js的waterline提供了声明式的关系型对象与DB的映射器,惊为天人,可以说是极大地提升了效率. 利用waterline的对象关系模型,用户可以直接使用javascript语 ...