Post Office
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 22278   Accepted: 12034

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

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])

数据加强版点这里Accepted2016-03-26

 

【代码】

#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的更多相关文章

  1. POJ 1160 Post Office (动态规划)

    Post Office Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15412   Accepted: 8351 Desc ...

  2. POJ 1160 Post Office(区间DP)

    Description There is a straight highway with villages alongside the highway. The highway is represen ...

  3. POJ 1160 Post Office(DP+经典预处理)

    题目链接:http://poj.org/problem?id=1160 题目大意:在v个村庄中建立p个邮局,求所有村庄到它最近的邮局的距离和,村庄在一条直线上,邮局建在村庄上. 解题思路:设dp[i] ...

  4. poj 1160 Post Office (间隔DP)

    Post Office Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 15966   Accepted: 8671 Desc ...

  5. POJ 1160 Post Office (四边形不等式优化DP)

    题意: 给出m个村庄及其距离,给出n个邮局,要求怎么建n个邮局使代价最小. 析:一般的状态方程很容易写出,dp[i][j] = min{dp[i-1][k] + w[k+1][j]},表示前 j 个村 ...

  6. POJ 1160 Post Office

    题意:有n个村庄,要在其中m个村庄里建邮局,每个村庄去邮局的代价为当前村庄到最近的一个有邮局村庄的路程,问总最小代价是多少. 解法:dp.dp[i][j]表示在前j个村庄建立i个邮局后的代价,则状态转 ...

  7. poj 1160 Post Office 【区间dp】

    <题目链接> 转载于:>>> 题目大意: 一条高速公路,有N个村庄,每个村庄均有一个唯一的坐标,选择P个村庄建邮局,问怎么选择,才能使每个村庄到其最近邮局的距离和最小?最 ...

  8. POJ.1160.Post Office(DP 四边形不等式)

    题目链接 \(Description\) 一条直线上有n个村庄,位置各不相同.选择p个村庄建邮局,求每个村庄到最近邮局的距离之和的最小值. \(Solution\) 先考虑在\([l,r]\)建一个邮 ...

  9. POJ——T 1160 Post Office

    http://poj.org/problem?id=1160 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20218   ...

随机推荐

  1. Spark:几种给Dataset增加列的方式、Dataset删除列、Dataset替换null列

    几种给Dataset增加列的方式 首先创建一个DF对象: scala> spark.version res0: String = .cloudera1 scala> val , , 2.0 ...

  2. grid - 通过网格线号码来定位网格项目

    网格线实际上是代表线的开始.结束. 两者之间就是网格列或行. 以下css仅对子元素生效 ,具体详情可以看后面示例 grid-row-start: 2; grid-row-end: 3; grid-co ...

  3. Spring MVC实现上传文件报错解决方案

    报错代码: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.sp ...

  4. java 除法运算只保留整数位的3种方式

      1.情景展示 根据提供的毫秒数进行除法运算,如果将毫秒数转换成小时,小时数不为0,则只取整数位,依此类推... 2.情况分析 可以使用3个函数实现 Math.floor(num)  只保留整数位 ...

  5. C语言100个经典的算法

    C语言的学习要从基础開始.这里是100个经典的算法-1C语言的学习要从基础開始,这里是100个经典的算法 题目:古典问题:有一对兔子,从出生后第3个月起每一个月都生一对兔子.小兔 子长到第三个月后每一 ...

  6. Django Web开发学习笔记(4)

    第四章 模板篇 上一章的内容,我们将HTML的代码和Python代码都混合在了在view.py的文件下.但是这样做的坏处无疑是明显的,引用DjangoBook的说法: 对页面设计进行的任何改变都必须对 ...

  7. [ci] jenkins kubernetes插件配置(容器模式)-通过jnlp

    有个小伙用sh结合jenkins搞的k8s cicd还不错 jenkins kubernetes插件 首先插件管理,搜索kubernetes plugin安装 配置kubernetes云 配置项目 执 ...

  8. 【工具】我的Git学习日志

    使用github一段时间,一直使用的是可视化工具,配合公司转用git,提前联系下git的命令. 安装 windows上安装git 从git for windows下载安装包,我下的是Git-2.13. ...

  9. 【Linux高级驱动】rtc驱动开发

    [1.分层思想] 1.1 rtc-dev.c   //设备接口层,功能:给用户提供接口 subsys_initcall(rtc_init);   , RTC_DEV_MAX, "rtc&qu ...

  10. el表达式字符串与变量拼接

    ${empty navigationMenu.pageid? '':'&mpage='.concat(navigationMenu.pageid)}