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. ArcGIS Pro 中不可用的工具

    有些可用于 ArcMap 之类的其他 ArcGIS Desktop 应用程序的地理处理工具在 ArcGIS Pro 中不可用.用于处理 ArcGIS Pro 所不支持的数据格式的地理处理工具已被移除, ...

  2. electron实现类似QQ来新消息时的闪烁与任务栏窗口提醒

    公司项目有一款带即时聊天.群组功能的APP,因为要给客服人员使用,需要开发PC版本.之前使用C#开发过一个PC版本,但是C#的UI这一块支持的不太好,而且升级比较麻烦,我就牵头基于Electron去实 ...

  3. Tensorflow 之finetune微调模型方法&&不同层上设置不同的学习率

    在不同层上设置不同的学习率,fine-tuning https://github.com/dgurkaynak/tensorflow-cnn-finetune ConvNets: AlexNet VG ...

  4. 开源的.NET系统推荐

    C# 源码 AForge.NET     RPC(Remote Procedure Call Protocol)远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的 ...

  5. Windows下Kettle定时任务执行并发送错误信息邮件

    Windows下Kettle定时任务执行并发送错误信息邮件 1.首先安装JDK 2.配置JDK环境 3.下载并解压PDI(kettle) 目前我用的是版本V7的,可以直接百度搜索下载社区版,企业版收费 ...

  6. ZOJ 3827 Information Entropy 水

    水 Information Entropy Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Informati ...

  7. 基于Centos搭建 Mono 开发环境

    系统要求: CentOS 7.2 64 位操作系统 安装 Mono 安装前的准备 yum install yum-utils 执行命令添加安装包仓库 rpm --import "http:/ ...

  8. Android studio3.1.3 打包jar,混淆

    最近公司需要将数据进行打包提供给用户,需要我们提供数据解析的jar给用户,为了防止数据格式的泄露,需要进行混淆.这里记录一下封装jar并混淆的过程. 1.创建module 之后创建了几个需要演示混淆的 ...

  9. C# ToShortDateString() ToString() 设置日期格式的区别

    在C#中,ToShortDateString()是用于显示短日期格式的方法,如果使用下面的语句: Label1.Text = DateTime.Now.ToShortDateString(); 那么, ...

  10. react学习笔记1之声明组件的两种方式

    //定义组件有两种方式,函数和类 function Welcome(props) { return <h1>Hello, {props.name}</h1>; } class ...