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. 为Titanium创建自己的安卓推送模块

    在手机应用中,推送是一个非常重要的功能.相对来说ios应用的推送功能很容易做,因为它统一都是用苹果的APNS服务实现的.但安卓这边就比较混乱了,虽然谷歌也推出了类似苹果的官方推送服务,但由于谷歌的服务 ...

  2. 深入理解javascript(一)

    此段文章摘自大叔的博客: 此文的目的是书写可维护的javascript代码. 最小的全局变量: JavaScript通过函数管理作用域.在函数内部声明的变量只在这个函数内部,函数外面不可用.另一方面, ...

  3. javascript 对象初探 (四)--- 内建对象之旅之Array

     我们不要去纠结神马是内建对象,神马是內建构造器.到后来你们便会发现其实她们都是对象. Array()是一个构建数组的內建构造器函数: var arr = new Array(); 与下面的是等效的: ...

  4. Java 静态语句块、语句块、构造函数执行顺序

    class Parent{ static String name = "hello"; { System.out.println("3 parent block" ...

  5. Swift获取屏幕快照

    // 获取屏幕快照 private func screenShot() -> UIImage{ let window = UIApplication.shared.keyWindow! UIGr ...

  6. 开发Android系统内置应用小记

    Android系统内置应用可以使用更多的API.更高的权限,与开发普通应用最大的差别在于编译,内置应用编译需要用到Android.mk文件.下面是我在开发过程中的一些小记. 1.在AndroidMai ...

  7. iOS 获取设备版本型号

    #import "sys/utsname.h" /** *  设备版本 * *  @return e.g. iPhone 5S */+ (NSString*)deviceVersi ...

  8. iOS拨打电话的三种方式

    iOS拨打电话的三种方式 1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示 1 2 var string = "tel:" + "1 ...

  9. Kotlin偏好设置

    Kotlin的强悍震精了我,android中每个应用都会用到SharedPreference在Kotlin中使用竟是如此简单! package com.android.extkt import and ...

  10. HTTP和GET/POST请求(NSURLConnection)

    网络编程 网络编程是一种实时更新应用数据的常用手段 网络编程是开发优秀网络应用的前提和基础 网络基本概念 客户端(就是手机或者ipad等手持设备上面的APP) 服务器(远程服务器-本地服务器) 请求( ...