[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 ...
随机推荐
- 解决微信小程序ios端滚动卡顿的问题
方案1:直接使用微信小程序提供的 “scroll-view " 组件. <scroll-view scroll-y style="height: 100%;"> ...
- js处理时间时区问题
问题背景:服务器时间是东八区时间,页面会在全世界各地,页面 JS 功能需要对比服务器时间和用户本地时间,为兼容世界各地时间,需要将用户本地时间转换为东八区时间 一.基本概念 1.格林威治时间 格林威治 ...
- AI通过了艺术创作图灵测试,你根本分不出来作者是不是人
各位geek朋友们,今年不用再看画了:近年来最大的艺术成就已经发生了. 这项艺术成就的诞生地,不是北京.新加坡.柏林郊区颜料四溅的画室中,不是威尼斯双年展上.请记住它出现的地点:美国新泽西州新布朗斯维 ...
- WPF应用程序exe接收参数
using System;using System.ServiceProcess; namespace GoShopService{ public partial class Service1 ...
- error C2039: 'SetWindowTextA' : is not a member of 'CString'
m_OpenPath.SetWindowText(strPath); 错误原因:在给控件关联变量m_OpenPath时,变量类型选择错误 解决办法:
- ASP.NET MVC ViewBag/ViewData/TempData区别
ViewBag/ViewData public dynamic ViewBag { get; } public ViewDataDictionary ViewData { get; set; } Vi ...
- ajax之async属性
Ajax请求中的async:false/true的作用 官方的解释是:http://api.jquery.com/jQuery.ajax/ async Boolean Default: true By ...
- [Aaronyang] 写给自己的WPF4.5 笔记7[三巴掌-ItemsControl数据绑定详解与binding二次处理 3/3]
我要做回自己--Aaronyang的博客(www.ayjs.net) 博客摘要: 全方位的讲解了转换器的使用,单值,多值转换器,条件转换器,StringFormat等方式 详细的实践地讲解了Items ...
- Golang——垃圾回收GC(2)
1 垃圾回收中的重要概念 1.1 定义 In computer science, garbage collection (GC) is a form of automatic memory manag ...
- Xilinx 常用模块汇总(verilog)【03】
作者:桂. 时间:2018-05-10 2018-05-10 21:03:44 链接:http://www.cnblogs.com/xingshansi/p/9021919.html 前言 主要记 ...