http://poj.org/problem?id=1160

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 20218   Accepted: 10899

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

 
 
minx[i][j]表示在i到j个村庄中,放1个邮局的最小距离和、
   当 i 到 j 有三个村庄,a1, a2, a3 那么邮局一定选在a2,当有a1,a2,a3,a4时,选在a2,a3是等效的,增加a5时,选在a3,最优,
    以此类推,得到 minx[i][j] = minx[i][j-1]+x[j]-x[i+j>>1] )
f[i][k]表示,从第1个村庄到第i个村庄放了k个邮局的最小距离和、
那么 f[i] [k] =min{ f[j] [k-1]+minx[j+1][i] }(j<i)
 
 #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的更多相关文章

  1. 【POJ】1160 Post Office

    http://poj.org/problem?id=1160 题意:直线上有n个城市,其中有p个城市有邮局,问如何建p个邮局使得每个城市到最近的邮局和最小.(n<=300, p<=30&a ...

  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 (动态规划)

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

  5. poj 1160 Post Office (间隔DP)

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

  6. [IOI 2000]POJ 1160 Post Office

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

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

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

  8. POJ 1160 Post Office

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

  9. poj 1160 Post Office 【区间dp】

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

随机推荐

  1. PCB 自动发送邮件---加入表格实现方法

    先看一下手动发送邮件内容加入表格操作(下图所示),直接复制Excel内容,再粘贴到邮件内容中,就是这么便捷,如果我们想自动发送邮件,也实现同样的效果如果实现呢,在这里介绍2种方法: 一.读取Excel ...

  2. Linux文件属性相关补充及软硬连接

    第1章 文件属性相关 1.1 文件的属性 1.1.1 扩展名 windows  通过扩展名区分不同的类型的文件 linux 扩展名是给人类看的 方便我们区分不同类型文件 .conf      配置文件 ...

  3. 产生冠军(toposort)

    http://acm.hdu.edu.cn/showproblem.php?pid=2094 #include <stdio.h> #include <iostream> #i ...

  4. [App Store Connect帮助]三、管理 App 和版本(6.2)转让 App:App 转让条件

    App 必须满足以下条件才可转让: 转让者和接收者的帐户均不处于待处理或变更状态,且双方均已接受“协议.税务和银行业务”部分中最新版本的转让协议 该 App 的任何版本都不能使用 iCloud 权限 ...

  5. 在chrome里模拟调试微信浏览器

    开发者模式(下面有配图): 开发者模式/DevTools.More tools/Network conditions/User agent/ Custom/安卓或ios代理配置配置 更改User ag ...

  6. Educational Codeforces Round 24 题解

    A: 考你会不会除法 //By SiriusRen #include <bits/stdc++.h> using namespace std; #define int long long ...

  7. HDU 5279 分治NTT 图的计数

    思路: 显然每个子图内都是森林 去掉所有子图1和n都连通且每条大边都存在的情况 直接DP上 NTT优化一波 注意前两项的值.. //By SiriusRen #include <bits/std ...

  8. Application、Activity Stack 和 Task的区别

    Application类 Application和Activity,Service一样是Android框架的一个系统组件,当Android程序启动时系统会创建一个Application对象,用来存储系 ...

  9. 大话设计模式--DI(依赖注入)

    1.背景 想象一个场景:有个功能通过某个参数决定了路由到不同的方法上或者几个方法模块可以自由搭配,咋办?一般人会对每个方法写一个helper(比如SendMessageForEmail.SendMes ...

  10. MVC系列学习(八)-分布视图

    1.本次学习实例 1.1.建议:为了尽可能让项目简单,就新建一个空的mvc项目,同时添加任何视图不用模板页 1.2注意:在添加LoginPart的分部视图时,要记得沟一个沟 2.项目代码,如下 总共三 ...