Post Office
| Time limit:1000 ms |
|
Memory limit:10000 K |
| Total submissions:15966 |
|
Accepted:8671 |
Description
There is a straight highway with ages 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 ages 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 ages. A village and the post office in it have the same position. for building the post offices, their positions shocould 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 versions 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 ages 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 ages. 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 51 2 3 6 7 9 11 22 44 50
Sample output
9
Ideas:
Sum [I] [J]: the shortest path for building a post office between village I and Village J;
[1, 3] The Post Office is built in 2nd villages;
[] The Post Office is built at the same distance of 2 and 3;
[1, 5] sum [1] [5] = sum [1] [4] + the distance between 5 and 3 in the village;
That is: sum [I] [J] = sum [I] [J-1] + X [J]-X [(I + J)/2];
DP [I] [J]: The minimum distance for setting up j post offices in the first village;
DP [I] [J] = min (DP [I] [J], DP [k] [J-1] + sum [k + 1] [I]); that is to say, building a post office in the village of [1, K] and building a post office in the village of [k + 1, I]
# Include <stdio. h> # include <math. h> # include <string. h> # include <stdlib. h >#include <algorithm> using namespace STD; # define n 305 const int INF = 0x3fffffff; int DP [N] [35]; // The minimum consumption of setting up j post offices in the first village: int sum [N] [N]; // sum [I] [J]: the shortest distance from village I to Village J to build a post office int X [N]; // The village location int main () {int V, p, I, j, k; while (scanf ("% d", & V, & P )! =-1) {for (I = 1; I <= V; I ++) scanf ("% d", & X [I]); memset (sum, 0, sizeof (SUM); for (I = 1; I <= V; I ++) {for (j = I + 1; j <= V; j ++) {sum [I] [J] = sum [I] [J-1] + X [J]-X [(I + J)/2];} for (I = 1; I <= V; I ++) {DP [I] [I] = 0; // The distance between a post office and a village is zero DP [I] [1] = sum [1] [I]; a post office is created in the previous village} For (j = 2; j <= P; j ++) {for (I = J + 1; I <= V; I ++) {DP [I] [J] = inf; for (k = J-1; k <I; k ++) {DP [I] [J] = min (DP [I] [J], DP [k] [J-1] + sum [k + 1] [I]) ;}} printf ("% d \ n ", DP [v] [p]);} return 0 ;}
Poj 1160 Post Office (interval Dynamic Planning)