首页 技术 正文
技术 2022年11月12日
0 收藏 881 点赞 4,678 浏览 2224 个字

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

IOI 2000 基本同山区建小学。

 #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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,565
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905