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. Firebug控制台详解(转)

    本文转自:http://www.ruanyifeng.com/blog/2011/03/firebug_console_tutorial.html 作者: 阮一峰 日期: 2011年3月26日 Fir ...

  2. 网络:W5500用浏览器配置设备

    1.背景 嵌入式端使用网络通信后,可以在PC端进行设备配置.方法有二:1)上位机配置:2)浏览器配置. 上位机配置可以把设置和测量作为一体,功能可以很强大,体验较好. 浏览器配置就是在电路板上搭载一个 ...

  3. 关于git bash的问题,pull不下来(登录之后,git帮你记住了,想切换其他用户)

    参考博客: https://www.jianshu.com/p/8a7f257e07b8 从某个项目地址pull代码下来,老是报错 fatal: Authentication failed for ' ...

  4. 微软名人数据集 ms_celeb_1m 处理(MsCelebV1-Faces-Aligned.tsv)python脚本

    本文主要介绍了如何对MsCelebV1-Faces-Aligned.tsv文件进行提取 原创by南山南北秋悲 欢迎引用!请注明原地址 http://www.cnblogs.com/hwd9654/p/ ...

  5. sublime text3 破解, 中文乱码支持, 设置

    1. 激活 菜单: Help -> Enter License, 弹出对话框输入激活码确认(Use License):如下图:. 激活码: ----- BEGIN LICENSE ----- A ...

  6. redis安装优化:

    1)内存分配控制: vm.overcommit_memoryredis启动时肯呢个会出现这样的日志: :M Apr ::! Background save may fail under low mem ...

  7. Spark 实现自定义对象sequenceFile方式存储,读写示例(scala编写)

    package com.fuge.bigdata.datahub.analysis import java.io.{DataInput, DataOutput} import com.fuge.big ...

  8. QT的基本数据类型

    QT的基本数据类型(转) qint8:signed char 有符号8比特数据 qint16:signed short 16位数据类型 qint32:signed int. 32位有符号数据类型 qi ...

  9. 1001: [BeiJing2006]狼抓兔子

    1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec  Memory Limit: 162 MBSubmit: 12827  Solved: 3044[Submit][ ...

  10. JAVA基础补漏--多态

    Fu obj = new ZI(); 访问成员变量规则 编译看左,运行看左. obj.num; 1.直接通过对象名访问成员变量:看等号左右是谁,优先用谁,没有则往上找. obj.getnum(); 2 ...