Post Office
Time limit:1000ms Memory limit:10000k
Description
There is a straight highway with villages alongside the highway. The highway is represented as a integer axis, and the position of each village are identified with a single integer Coordi Nate. There is no villages in the same position. The distance between and positions is the absolute value of the difference of the their integer coordinates.
Post Offices is built in some and not necessarily all of the villages. A Village and the post office in it has the same position. For building the post offices, their positions should is chosen so, the total sum of all distances between each villag E and its nearest post office is minimum.
You is to write a program which, given the positions of the villages and the number of post offices, computes the least P Ossible sum of all distances between, village and its nearest post office.
Input
Your program was to read from standard input. The first line contains integers:the first was the number of villages V, 1 <= v <=, and the second is the Nu Mber of Post offices p, 1 <= p <=, p <= v. The second line contains V integers in increasing order. These V integers is the positions of the villages. For each position x it holds 1 <= x <= 10000.
Output
The first line contains one integer S, which are 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
Test Instructions: Given the absolute location of N cities, the establishment of M post offices in some cities makes all cities and the nearest post office the smallest distance.
idea: before doing this DP, mathematical knowledge: The median distance from all other numbers and the shortest distance.
Proof can look here: HDU-1227
The idea is almost identical to the code.
The code is as follows:
/* * ID:J.SURE.1 * PROG: * lang:c++ * *#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <ctime>#include <cmath>#include <stack>#include <queue>#include <vector>#include <map>#include <set>#include <string>#include <iostream>#define PB push_back#define LL Long Longusing namespace STD;Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-8;/****************************************/Const intN =305, M = *;intN, M;intW[n];intDp[m][n], cost[n][n];voidPre_treat () { for(inti =1; I <= N; i++) { for(intj =1; J <= N; J + +) {Cost[i][j] =0;intMid = (i+j) >>1; for(intK = i; K <= j;k++) {Cost[i][j] + =ABS(W[mid]-w[k]); } } }}intSolve () { for(intj =1; J <= N; J + +) {dp[1][J] = cost[1][J]; } for(inti =2; I <= m; i++) { for(intj =1; J <= N; J + +) {intret = INF; for(intK =1; K < J; k++) {ret = min (ret, dp[i-1][K] + cost[k+1][J]); } Dp[i][j] = ret; } }returnDp[m][n];}intMain () {#ifdef j_sureFreopen ("000.in","R", stdin);//freopen ("999.out", "w", stdout);#endif scanf("%d%d", &n, &m); for(inti =1; I <= N; i++) {scanf("%d", &w[i]); } pre_treat ();intans = solve ();printf("%d\n", ans);return 0;}
"Dp| Math + preprocessing" POJ-1160 Post Office