POJ——T 1160 Post Office
http://poj.org/problem?id=1160
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 20218 | Accepted: 10899 |
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
Output
Sample Input
10 5
1 2 3 6 7 9 11 22 44 50
Sample Output
9
Source
#include <cstring>
#include <cstdio> inline void read(int &x)
{
x=; register char ch=getchar();
for(; ch>''||ch<''; ) ch=getchar();
for(; ch>=''&&ch<=''; ch=getchar()) x=x*+ch-'';
} const int N();
#define min(a,b) (a<b?a:b)
int n,m,x[N],minx[N][N],f[N][N]; int Presist()
{
// freopen("post.in","r",stdin);
// freopen("post.out","w",stdout); read(n),read(m);
if(m==n) { puts(""); return ; }
for(int i=; i<=n; ++i) read(x[i]);
for(int i=; i<=n; ++i)
for(int j=i+; j<=n; ++j)
minx[i][j]=minx[i][j-]+x[j]-x[i+j>>];
memset(f,/,sizeof(f));
for(int i=; i<=n; ++i)
f[i][]=minx[][i],f[i][i]=;
for(int k=; k<=m; ++k)
for(int i=; i<=n; ++i)
{
for(int j=i-; j; --j)
f[i][k]=min(f[i][k],f[j][k-]+minx[j+][i]);
}
printf("%d\n",f[n][m]);
return ;
} int Aptal=Presist();
int main(){;}
POJ——T 1160 Post Office的更多相关文章
- 【POJ】1160 Post Office
http://poj.org/problem?id=1160 题意:直线上有n个城市,其中有p个城市有邮局,问如何建p个邮局使得每个城市到最近的邮局和最小.(n<=300, p<=30&a ...
- 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 (动态规划)
Post Office Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 15412 Accepted: 8351 Desc ...
- poj 1160 Post Office (间隔DP)
Post Office Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 15966 Accepted: 8671 Desc ...
- [IOI 2000]POJ 1160 Post Office
Post Office Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22278 Accepted: 12034 Descrip ...
- 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个村庄建邮局,问怎么选择,才能使每个村庄到其最近邮局的距离和最小?最 ...
随机推荐
- 湖南集训day4
难度:☆☆☆☆☆☆☆ 题解: 有个定理,另sum(x)表示小于等于x的数中与x互质的数的和 sum(x)=φ(x)*x/2 最后可知f(x)=x (f(1)=2) 当然打表能知道. 然后就转 ...
- Java数组去重(利用数组,不借助集合)
今天有个同学问我说老师,Java里边数组怎么去重,在不借助List集合的情况下,最后呢我整理了一下,打算发一篇博文,希望能帮助到有用的人,大佬绕过 public static void arrUniq ...
- 记一次Oracle冷备恢复的过程
一.故障来临 某日中午,市电意外中断,机房UPS电源由于负载过重而未接管供电,所有服务器全部重启...... 待所有服务器重启后,正在逐一检查设备和业务运行情况时,意外发生了.一台年代久远的HP PC ...
- [转]mysql索引详解
转自:http://www.cnblogs.com/ggjucheng/archive/2012/11/04/2754128.html 什么是索引 索引用来快速地寻找那些具有特定值的记录,所有MySQ ...
- [转]Oracle Client 11g安裝經驗
本文转自:http://www.dotblogs.com.tw/shadow/archive/2011/11/08/54759.aspx 開發環境:本機(Win XP)的ASP.net 4 WebSi ...
- ResGen.exe”已退出,代码为2 问题处理
转载自 http://blog.sina.com.cn/s/blog_5f82a1060101d8tm.html 在64位的Windows 7下,用VS2010编译4.0以前的.Net项目会有问题. ...
- Extension Methods(扩展方法)
在 OOPL 中,有静态方法.实例方法和虚方法,如下: public sealed class String { public static bool IsNullOrEmpty(st ...
- netty学习:UDP服务器与Spring整合(2)
上一篇文章中,介绍了netty实现UDP服务器的栗子. 本文将会对UDP服务器与spring boot整合起来,并使用RedisTemplate的操作类访问Redis和使用Spring DATA JP ...
- 转 方法区(method) )、栈区(stack)和堆区(heap)之JVM 内存初学
JAVA的JVM的内存可分为3个区:堆(heap).栈(stack)和方法区(method) 堆区: 1.存储的全部是对象,每个对象都包含一个与之对应的class的信息.(class的目的是得到操作指 ...
- [ NOIP 2014 ] TG
\(\\\) \(Day\ 1\) \(\\\) \(\#\ A\) \(Rps\) 定义五种方案的石头剪刀布游戏,两人共进行\(N\)局游戏,已知两人各自的循环节和具体方案,胜者得\(1\)分,败者 ...