Post Office
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 18835   Accepted: 10158

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

 
基本同山区建小学。
 #include "cstdio"
#include "cstring"
#include "cstdlib"
#include "iostream" using namespace std ;
const int INF = ;
const int maxN = 1e3 ; int d [ maxN ] , f [ maxN ][ maxN ] , cost[ maxN ][ maxN ] ; int gmin ( int x , int y ) { return x > y ? y : x ; } inline int INPUT ( ) {
int x = , F = ; char ch = getchar ( ) ;
while ( ch < '' || '' < ch ) { if ( ch == '-' ) F = - ; ch = getchar ( ) ; }
while ( '' <= ch && ch <= '' ) { x = ( x << ) + ( x << ) + ch - '' ; ch = getchar ( ) ; }
return x * F ;
} void calc ( int M ) {
for ( int i= ;i<=M ; ++i ) {
for ( int j=i+ ; j<=M ; ++j ){
int Dis = ;
int mid = ( i + j ) >> ;
for ( int k=i ; k<mid ; ++k )Dis += d[mid] - d[k] ;
for ( int k=mid+ ; k<=j ; ++k )Dis += d[k] - d[mid] ;
cost[ i ][ j ] = Dis ;
}
}
} void Init_2 ( int N , int M ) {
for ( int i= ; i<=M ; ++i ) {
for ( int j= ; j<=N ; ++j ) {
if ( j== ) f[ i ][ j ] = cost[ ][ i ] ;
else f[ i ][ j ] = INF ;
}
}
}
void Init_1 ( int M ) {
for ( int i= ; i<=M; ++i )
for ( int j=i+ ; j<=M; ++j )
cost[ i ][ j ] = INF ;
} int main ( ) {
int M = INPUT ( ) , N = INPUT ( ) ;
for ( int i= ; i<=M ; ++i )
d[ i ] = INPUT ( ) ;
Init_1( M ) ;
calc ( M ) ;
Init_2 ( N , M ) ;
for ( int i= ; i<=M ; ++i ) {
for ( int j= ; j<=N ; ++j ) {
for ( int k= ; k<i ; ++k ){
f[ i ][ j ] = gmin ( f[ i ][ j ] , f[ k ][ j - ] + cost[ k + ][ i ] ) ;
}
}
}
printf ( "%d\n" , f[ M ][ N ] ) ;
return ;
}

2016-10-20  21:01:34

POJ 1160 题解的更多相关文章

  1. POJ 1160 Post Office(区间DP)

    Description There is a straight highway with villages alongside the highway. The highway is represen ...

  2. POJ 1160 DP

    题目: poj 1160 题意: 给你n个村庄和它的坐标,现在要在其中一些村庄建m个邮局,想要村庄到最近的邮局距离之和最近. 分析: 这道题.很经典的dp dp[i][j]表示建第i个邮局,覆盖到第j ...

  3. POJ 1160 经典区间dp/四边形优化

    链接http://poj.org/problem?id=1160 很好的一个题,涉及到了以前老师说过的一个题目,可惜没往那上面想. 题意,给出N个城镇的地址,他们在一条直线上,现在要选择P个城镇建立邮 ...

  4. POJ 1160 Post Office(DP+经典预处理)

    题目链接:http://poj.org/problem?id=1160 题目大意:在v个村庄中建立p个邮局,求所有村庄到它最近的邮局的距离和,村庄在一条直线上,邮局建在村庄上. 解题思路:设dp[i] ...

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

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

  6. poj 3744 题解

    题目 题意: $ yyf $ 一开始在 $ 1 $ 号节点他要通过一条有 $ n $ 个地雷的道路,每次前进他有 $ p $ 的概率前进一步,有 $ 1-p $ 的概率前进两步,问他不领盒饭的概率. ...

  7. poj 3061 题解(尺取法|二分

    题意 $ T $ 组数据,每组数据给一个长度 $ N $ 的序列,要求一段连续的子序列的和大于 $ S $,问子序列最小长度为多少. 输入样例 2 10 15 5 1 3 5 10 7 4 9 2 8 ...

  8. POJ 3977 题解

    题目 Given a list of N integers with absolute values no larger than \(10^{15}\), find a non empty subs ...

  9. DP中环形处理 +(POJ 1179 题解)

    DP中环形处理 对于DP中存在环的情况,大致有两种处理的方法: 对于很多的区间DP来说,很常见的方法就是把原来的环从任意两点断开(注意并不是直接删掉这条边),在复制一条一模一样的链在这条链的后方,当做 ...

随机推荐

  1. 收藏的Android学习资源

    1. Android中混合开发系列 Android中Java与JavaScript的交互,可用来实现网页与本地APP的交互,信息的传递等: 谈谈App的混合开发,可以让一部分功能由html5开发,这部 ...

  2. BZOJ 3160: 万径人踪灭

    Description 一个ab串,问有多少回文子序列,字母和位置都对称,并且不连续. Sol FFT+Manacher. 不连续只需要减去连续的就可以了,连续的可以直接Manacher算出来. 其他 ...

  3. Python复习之生成器 generator

    生成器 generator def h(): print "hello" m = yield 5 print "m:", m d = yield 12 prin ...

  4. git 修改注释信息

    1. push 之前 先看看自己提交过多少次,然后执行 git rebase -i HEAD~数字(你要修改你的第几次提交) 接下来执行,修改注释 git commit --amend 修改完注释之后 ...

  5. 记录NS_ASSUME_NONNULL_BEGIN和NS_ASSUME_NONNULL_END。

    Nonnull区域设置(Audited Regions) 如果需要每个属性或每个方法都去指定nonnull和nullable,是一件非常繁琐的事.苹果为了减轻我们的工作量,专门提供了两个宏:NS_AS ...

  6. SqlServer 2008登录时报错

    登录SQLServer2008R2时提示如下错误: 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务器.请验证实例名称是否正确并且 SQL Server ...

  7. 如何在eclipse中通过Juit进行单元测试

    1.什么是Junit Junit即单元测试,是JAVA语言的单元测试框架,是对程序的一个方法所进行的测试 一般都是由程序员自己通过Junit来进行测试,因此单元测试也叫程序员测试: 如果测试人员熟悉程 ...

  8. Clang与libc++abi库安装

    系统ubuntu64位 Clang4.0 参考: 1 https://github.com/yangyangwithgnu/use_vim_as_ide#0.1 其中 第7章 工具链集成 2. htt ...

  9. 【笔记】读取properties文件

    package com.bshinfo.el.userInfo.util; import java.io.BufferedReader; import java.io.File; import jav ...

  10. 51nod1185(wythoff+高精度)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1185 题意:中文题诶- 思路:wythoff模板题,和51n ...