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

n个绿色点选m个染成红点,使得每个点到最近的红点距离和最小;

划分区间的时候,c[i][j]的为i到j染一个点的最优解(很好的思想)。

暴力的DP:

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int inf=;
const int maxn=;
int sum[maxn],a[maxn],dp[maxn][];
int c[maxn][maxn];
int main()
{
int n,m,i,j,k;
scanf("%d%d",&n,&m);
for(i=;i<=n;i++) scanf("%d",&a[i]);
for(i=;i<=n;i++)
for(j=;j<=m;j++) dp[i][j]=inf; sort(a+,a+n+);
for(i=;i<=n;i++)
for(j=i+;j<=n;j++){
int mid=(i+j)/;
for(k=i;k<=j;k++)
c[i][j]+=abs(a[k]-a[mid]);
}
for(i=;i<=n;i++) dp[i][]=c[][i];
for(i=;i<=n;i++)
for(j=;j<=m;j++){
for(k=;k<i;k++)
dp[i][j]=min(dp[i][j],dp[k][j-]+c[k+][i]);
}
printf("%d\n",dp[n][m]);
return ;
}

优化的DP:

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int inf=;
const int maxn=;
int sum[maxn],a[maxn],dp[][maxn];
int c[maxn][maxn],s[maxn][maxn];
int main()
{
int n,m,i,j,k;
scanf("%d%d",&n,&m);
for(i=;i<=n;i++) scanf("%d",&a[i]);
for(i=;i<=m;i++)
for(j=;j<=n;j++) dp[i][j]=inf; sort(a+,a+n+); for(i=;i<=n;i++)//可以换成公式而非暴力
for(j=i;j<=n;j++){
int mid=(i+j)/;
for(k=i;k<=j;k++)
c[i][j]+=abs(a[k]-a[mid]);
} for(i=;i<=n;i++) dp[][i]=c[][i];
for(i=;i<=m;i++) {
s[i][n+]=n;
for(j=n;j>=;j--) {
for(k=s[i-][j];k<=s[i][j+];k++) {
if(dp[i][j]>dp[i-][k]+c[k+][j]){
s[i][j] = k;
dp[i][j]=dp[i-][k]+c[k+][j];
}
}
}
}
printf("%d\n",dp[m][n]);
return ;
}

POJ1160 Post Office (四边形不等式优化DP)的更多相关文章

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

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

  2. 【转】斜率优化DP和四边形不等式优化DP整理

    (自己的理解:首先考虑单调队列,不行时考虑斜率,再不行就考虑不等式什么的东西) 当dp的状态转移方程dp[i]的状态i需要从前面(0~i-1)个状态找出最优子决策做转移时 我们常常需要双重循环 (一重 ...

  3. hdu 2829 Lawrence(四边形不等式优化dp)

    T. E. Lawrence was a controversial figure during World War I. He was a British officer who served in ...

  4. BZOJ1563/洛谷P1912 诗人小G 【四边形不等式优化dp】

    题目链接 洛谷P1912[原题,需输出方案] BZOJ1563[无SPJ,只需输出结果] 题解 四边形不等式 什么是四边形不等式? 一个定义域在整数上的函数\(val(i,j)\),满足对\(\for ...

  5. codevs3002石子归并3(四边形不等式优化dp)

    3002 石子归并 3 参考 http://it.dgzx.net/drkt/oszt/zltk/yxlw/dongtai3.htm  时间限制: 1 s  空间限制: 256000 KB  题目等级 ...

  6. CF321E Ciel and Gondolas Wqs二分 四边形不等式优化dp 决策单调性

    LINK:CF321E Ciel and Gondolas 很少遇到这么有意思的题目了.虽然很套路.. 容易想到dp \(f_{i,j}\)表示前i段分了j段的最小值 转移需要维护一个\(cost(i ...

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

    d(i, j)表示用i个邮局覆盖前j个村庄所需的最小花费 则有状态转移方程:d(i, j) = min{ d(i-1, k) + w(k+1, j) } 其中w(i, j)的值是可以预处理出来的. 下 ...

  8. HDU 2829 Lawrence (斜率优化DP或四边形不等式优化DP)

    题意:给定 n 个数,要你将其分成m + 1组,要求每组数必须是连续的而且要求得到的价值最小.一组数的价值定义为该组内任意两个数乘积之和,如果某组中仅有一个数,那么该组数的价值为0. 析:DP状态方程 ...

  9. 四边形不等式优化DP——石子合并问题 学习笔记

    好方啊马上就要区域赛了连DP都不会QAQ 毛子青<动态规划算法的优化技巧>论文里面提到了一类问题:石子合并. n堆石子.现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆石子合并成新的 ...

  10. 石子合并(四边形不等式优化dp) POJ1160

    该来的总是要来的———————— 经典问题,石子合并. 对于 f[i][j]= min{f[i][k]+f[k+1][j]+w[i][j]} From 黑书 凸四边形不等式:w[a][c]+w[b][ ...

随机推荐

  1. 单例模式及getInstance()的用法

    一般在单例模式下使用.getInstance()创建对象:但并不是所有有私有构造方法,对外通过getInstance方法提供 实例的情况就是单例模式. 注:单例模式:一个类有且只有一个实例.1,一个私 ...

  2. python中定制类

    1.python中__str__和repr 如果要把一个类的实例变成 str,就需要实现特殊方法__str__(): class Person(object): def __init__(self, ...

  3. Windows:FTP命令大全

    Windows:FTP命令大全 简介 1, open:与服务器相连接: 2, send(put):上传文件: 3,get:下载文件: 4,mget:下载多个文件: 用法: mget *:下载当前路径下 ...

  4. Nginx rewrite配置

    rewrite应用 Rewrite模块设置及Wordpress和Discuz的示例.Nginx的Rewrite规则比Apache的简单灵活多了,从下面介绍可见一斑. rewrite配置 Nginx可以 ...

  5. spark学习12(Wordcount程序之spark-shell)

    在目录/home/hadoop/2016113012下有文件words.txt hello scala hello java hello python hello wujiadong 上传该文件到hd ...

  6. JavaScript中this关键字的使用比较

    JavaScript中this关键字的使用比较 this关键字在JavaScript中,用的不能说比较多,而是非常多.那么熟悉this关键字的各种用法则显得非常关键. this有时候就是我们经常说的上 ...

  7. 某些编辑器运行C程序闪退的解决办法

    在某些C语言编辑器中运行C语言程序或点击生成的.exe文件出现闪退现象的解决办法,主要有两种,还有其它方法欢迎交流. 包含头文件<windows.h>,在程序末尾添加system(&quo ...

  8. scala学习手记11 - 类定义

    这里会通过与Java比较的方式来说明scala是如何创建类的. 先来看一下Java中是如何定义一个类的: public class Car { private final int year; priv ...

  9. angular-ui-bootstrap各版本下载地址

    http://www.bootcdn.cn/angular-ui-bootstrap/

  10. MFC--根据串口采集的数据借助GDI绘制曲线

    根据采集到的数据绘制曲线 在串口编程中会涉及到这样一个问题,就是将采集到的数据以曲线的形式展示出来,大家自然而然会想到采用方便快捷的控件进行编程.编程周期短,完成任务快,但是真实情况来看,控件会实现很 ...