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. 在Objective-C 中使用字符生成NSArray、NSDictionary、NSNumber

    @符号不仅可以生成字符串,还可以生成其他数据类型如NSArray.NSDictionary和NSNumber,是一种简洁快速的用法. // NSArray array = [NSArray array ...

  2. Android防止进程被第三方软件杀死

    http://blog.csdn.net/wangliang198901/article/details/12342845 http://stackoverflow.com/questions/385 ...

  3. 第十五章 dubbo结果缓存机制

    dubbo提供了三种结果缓存机制: lru:基于最近最少使用原则删除多余缓存,保持最热的数据被缓存 threadlocal:当前线程缓存 jcache:可以桥接各种缓存实现 一.使用方式 <du ...

  4. python BeautifulSoup的简单使用

    官网:https://www.crummy.com/software/BeautifulSoup/bs4/doc/ 参考:https://www.cnblogs.com/yupeng/p/336203 ...

  5. Fix Corrupt Blocks on HDFS

    来自:http://centoshowtos.org/hadoop/fix-corrupt-blocks-on-hdfs/ How do I know if my hadoop hdfs filesy ...

  6. CentOS6.9下安装python notebook

    操作系统:CentOS6.9_x64 python版本 : python2.7.13 添加低权限新用户: useradd mike su mike 使用virtualenv安装python2.7环境 ...

  7. PyCharm for Mac(Python 开发工具)破解版安装

    1.软件简介    PyCharm 是 macOS 系统上一款 Python 编辑利器,具有智能代码编辑器,能理解 Python 的特性并提供卓越的生产力推进工具:自动代码格式化.代码完成.重构.自动 ...

  8. Git 安装(分布式版本控制系统)

    1.在 Windows 上安装 在 Windows 上安装 Git 也有几种安装方法. 官方版本可以在 Git 官方网站下载,打开下载会自动开始.要注意这是一个名为 Git for Windows 的 ...

  9. [Aaronyang] 写给自己的WPF4.5 笔记16[多线程]

      =============潇洒的版权线==========www.ayjs.net===== Aaronyang ========= AY =========== 安徽 六安 杨洋 ======= ...

  10. numpy数组(4)-二维数组

    python创建二维 list 的方法是在 list 里存放 list : l = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]] numpy可以直接 ...