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. mysql win源码比较大 不需要的文件删除 记录下来

    mysql-5.6.25-win32.zip 从官方下载下来的大小为 343MB,太大了可以删除掉的地方记录一下 docs :全部删除. sql-bench:全部删除. mysql-test :全部删 ...

  2. cordova 添加闪屏效果

    为项目添加SplashScreen插件 在Cordova项目目录运行: cordova plugin add apache.cordova.splashscreen 这个命令从插件git库下载插件代码 ...

  3. SharePoint解决The security validation for this page is invalid.

    我是在一个service后台用object model去check in一个spfile的时候报的这个错.这是SharePoint的一种保护机制,在处理不能确定是安全的请求时,sharepoint就会 ...

  4. UITableView

    知识点: 1)UITableView 2)UITableViewCell ====================================================== 一.UITabl ...

  5. Xcode8以及iOS10问题总结!

    Xcode8的问题 注释功能问题解决 打开终端,命令运行: sudo /usr/libexec/xpccachectl 重启电脑 在xib和stroyboard出现大量警告,需要重新适配,字体所占用宽 ...

  6. ReactiveCocoa基础知识内容2

    引用网络上一些实例的代码,针对ReactiveCocoa的运用可以更加有帮助: 1:跟AF结合时的写法,返回RACSignal - (RACSignal *)fetchQuestionWithTag: ...

  7. Android Sqlite基本命令

    要查看数据库,首先必须要找到db文件,如果拷贝到电脑上,查看的方法比较多,在手机上,用命令查看比较直接和方便. 首先要找到数据库的位置,一般数据库时存放在程序的私有目录,所以要获取root权限. 确保 ...

  8. C# 依赖缓存

    使用轮询的方式 数据库: 在VS的命令里面输入 aspnet_regsql.exe -S (local) -U sa -P 123456 -d ERP_SQL -ed 上面这句是用来设置哪个服务器上的 ...

  9. mysql命令(数据库备份与恢复)

    本地: 1.进入MySQL目录下的bin文件夹:e:回车: e:\>cd mysql\bin? 回车 2.导出数据库:mysqldump -u 用户名 -p 数据库名 > 导出的文件名 范 ...

  10. [PL/SQL]oracle数据库的导出导入

    一.PL/SQL Developer工具一般对oracle的导入导出有以下4中方式: 1.Oracle导出导入方式 这种方式导出导入为.dmp的文件格式,.dmp文件是二进制的,可以跨平台,还能包含权 ...