Post Office
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 18680   Accepted: 10075

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

---------------------
在村庄内建邮局,要使村庄到邮局的距离和最小
---------------------
f[i][j]表示前i个村庄中建j个邮局的最小距离,考虑最后一个邮局的位置
f[i][j]=min{f[k][j-1]+l[k+1][i]}
l可以递推,建在中点最好,发现l[i][j]=l[i][j-1]+a[j]-a[(i+j)/2]成立
注意预处理
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int V=,P=,INF=1e9;
inline int read(){
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int v,p,a[V],l[V][V],f[V][P];
void dp(){
for(int i=;i<=v;i++)
for(int j=i+;j<=v;j++)
l[i][j]=l[i][j-]+a[j]-a[(i+j)/];
for(int i=;i<=v;i++) for(int j=;j<=v;j++) f[i][j]=INF,f[i][]=l[][i];
for(int i=;i<=v;i++)
for(int j=;j<=p;j++){
for(int k=;k<i;k++)
f[i][j]=min(f[i][j],f[k][j-]+l[k+][i]);
}
}
int main(int argc, const char * argv[]) {
v=read();p=read();
for(int i=;i<=v;i++)a[i]=read();
dp();
printf("%d",f[v][p]);
return ;
}

POJ1160 Post Office[序列DP]的更多相关文章

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

    题目大意:有v个村庄成直线排列,要建设p个邮局,为了使每一个村庄到离它最近的邮局的距离之和最小,应该怎样分配邮局的建设,输出最小距离和. 题目分析:定义状态dp(i,j)表示建设 i 个邮局最远覆盖到 ...

  2. 72. Edit Distance(困难,确实挺难的,但很经典,双序列DP问题)

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  3. [OpenJudge90][序列DP+乱搞]滑雪

    滑雪 总时间限制: 1000ms 内存限制: 65536kB [描述] Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当你滑到坡底,你不得不再次 ...

  4. 序列DP(输出有要求)

    DP Time Limit:10000MS     Memory Limit:165888KB     64bit IO Format:%lld & %llu Submit Status De ...

  5. hdoj5909 Tree Cutting(点分治+树上dp转序列dp)

    题目链接:https://vjudge.net/problem/HDU-5909 题意:给一颗树,结点带权值v[i]<m.求异或和为k的子树个数(0<=k<m). 思路: 首先点分治 ...

  6. 一类巧妙利用利用失配树的序列DP

    I.导入 求长度为\(\text{len}\)的包含给定连续子串\(\text{T}\)的 0/1 串的个数.(\(|T|<=15\)) 通常来说这种题目应该立刻联想到状压 DP 与取反集--这 ...

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

    There is a straight highway with villages alongside the highway. The highway is represented as an in ...

  8. [POJ1160] Post Office [四边形不等式dp]

    题面: 传送门 思路: dp方程实际上很好想 设$dp\left[i\right]\left[j\right]$表示前$j$个镇子设立$i$个邮局的最小花费 然后状态转移: $dp\left[i\ri ...

  9. 洛谷P1415 拆分数列[序列DP 状态 打印]

    题目背景 [为了响应党中央勤节俭.反铺张的精神,题目背景描述故事部分略去^-^] 题目描述 给出一列数字,需要你添加任意多个逗号将其拆成若干个严格递增的数.如果有多组解,则输出使得最后一个数最小的同时 ...

随机推荐

  1. LAMP一键安装脚本 from:秋水逸冰

    Install LAMP(Linux + Apache + MySQL + PHP ) for CentOS/Redhat/Fedora 项目地址:https://github.com/teddysu ...

  2. ionic + cordova 配置和开发过程中的一些问题

    1.Android sdk:ERROR: SWT folder '' does not exist.问题 在win x86系统中,如果运行Android的 sdk manager,会出现Android ...

  3. 2013最常用的NoSQL数据库

    摘要:与关系数据库相比,每个NoSQL都有自己不同的适用场景,这里带大家盘点文档数据库.图数据库.键值数据存储.列存储数据库与内存数据网络等领域的常用的NoSQL. 在几年内,NoSQL数据库一直以性 ...

  4. SharePoint 2013 表单认证使用ASP.Net配置工具添加用户

    前 言 上面一篇博客,我们了解到如何为SharePoint 2013配置表单身份认证,但是添加用户是一个麻烦事儿:其实,我们还可以用Asp.Net的配置工具,为SharePoint 2013添加表单用 ...

  5. 【代码笔记】iOS-给背景图赋值颜色

    代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // ...

  6. myIsEqualToString

    BOOL myisEqualToString(NSString * str1 , NSString * str2){ //1.如果两个字符串,指针地址相等,就说明一定是相等 if(str1 == st ...

  7. iOS 学习资源

    这份学习资料是为 iOS 初学者所准备的, 旨在帮助 iOS 初学者们快速找到适合自己的学习资料, 节省他们搜索资料的时间, 使他们更好的规划好自己的 iOS 学习路线, 更快的入门, 更准确的定位的 ...

  8. 【转】JavaScript 异步进化史

    前言 JS 中最基础的异步调用方式是 callback,它将回调函数 callback 传给异步 API,由浏览器或 Node 在异步完成后,通知 JS 引擎调用 callback.对于简单的异步操作 ...

  9. PlantUML的实例参考

    project: blog target: plant-uml-instances.md date: 2015-12-24 status: publish tags: - PlantUML - UML ...

  10. 在VC环境下执行代码出现错误

    这是在执行代码过程中出现的错误,源代码在别的电脑上能运行,在自己的VC里运行就出现错误,在网上也搜过解决办法,但还是有点不太理解,是编程环境的问题h还是代码本身也存在问题???