Poj1160 Post Office

Source: Internet
Author: User

Post Office
Time Limit: 1000 MS Memory Limit: 10000 K
Total Submissions: 14051 Accepted: 7576

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 5
1 2 3 6 7 9 11 22 44 50 Sample Output

9 Source

IOI 2000
Analysis: This is a very good dp question. First, dp [I] [j] indicates that j post offices are repaired from 0 to I, sum [I] [j] indicates the shortest path for building a post office from I to j!
Then dp [I] [j] = dp [k] [J-1] + sum [k + 1] [j] (k> = 0 k <= I );
Sum [I] [j] = sum [I] [J-1] + p [I]-p [(I + j)/2];
Let's talk about these two formulas!
First, we can draw a conclusion that the shortest method for building a post office in an even number of points is the two points in the middle, and the distance between the two points is equivalent!
Such as p0, p1, p2, p3, we build in p1, p2 which is A, are p3-p1 + p2-p0, this point can be promoted to all even points, odd points, it is also in the middle,
But there is only one point! For example, do we want to add a vertex? For example, in p0, p1, p2, p3 Add a p4, then how from sum [I] [J-1] To sum [I] [j? We can find that
You only need to add a distance from p4 to p2, that is, p [j]-p [(I + j)/2]. In fact, this example is very simple, however, it is also quite common. Let's try again, if it is
What about odd points? For example, p0, p1, p2, p3, p4, and a p5 are added. How can this problem be solved? We can obviously know that p [j]-p [(I + j)/2] is also added.
Let's talk about the first formula,
We can know that the shortest Method for putting j post offices from 0 to I must be k at a certain point from 0 to I, that is, dp [k] [J-1], because we can change k from 0
I, all enumeration, so that the first k points, put the j Post Office's shortest short circuit, plus a from k + 1 to j put a post office's shortest short circuit, you can get it! In this way, this problem is solved, but in extreme cases, dp [I] [0] = sum [0] [I], which needs to be initialized, this is!

# Include <iostream> # include <string. h> # include <stdio. h> using namespace std; # define inf 0x4f4f4f4f int dp [320] [320], sum [320] [320], p [320]; int fmin (int, int B) {if (a <B) return a; return B;} int main () {int n, m, I, j, k; while (scanf ("% d", & n, & m )! = EOF) {for (I = 0; I <n; I ++) {scanf ("% d", & p [I]);} memset (sum, 0, sizeof (sum); for (I = 0; I <n; I ++) for (j = I + 1; j <n; j ++) {sum [I] [j] = sum [I] [J-1] + p [j]-p [(I + j)/2];} for (I = 0; I <n; I ++) {dp [I] [0] = sum [0] [I]; for (j = 1; j <m; j ++) {dp [I] [j] = inf ;}for (I = 0; I <n; I ++) for (j = 1; j <m; j ++) for (k = 0; k <I; k ++) {dp [I] [j] = fmin (dp [I] [j], dp [k] [J-1] + sum [k + 1] [I]);} printf ("% d \ n", dp [n-1] [M-1]); // count, all starting from 0} return 0;} # include <ios Tream> # include <string. h> # include <stdio. h> using namespace std; # define inf 0x4f4f4f4fint dp [320] [320], sum [320] [320], p [320]; int fmin (int a, int B) {if (a <B) return a; return B;} int main () {int n, m, I, j, k; while (scanf ("% d", & n, & m )! = EOF) {for (I = 0; I <n; I ++) {scanf ("% d", & p [I]);} memset (sum, 0, sizeof (sum); for (I = 0; I <n; I ++) for (j = I + 1; j <n; j ++) {sum [I] [j] = sum [I] [J-1] + p [j]-p [(I + j)/2];} for (I = 0; I <n; I ++) {dp [I] [0] = sum [0] [I]; for (j = 1; j <m; j ++) {dp [I] [j] = inf ;}for (I = 0; I <n; I ++) for (j = 1; j <m; j ++) for (k = 0; k <I; k ++) {dp [I] [j] = fmin (dp [I] [j], dp [k] [J-1] + sum [k + 1] [I]);} printf ("% d \ n", dp [n-1] [M-1]); // count, all starting from 0} return 0 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.