POJ 1160 Post Office (Post office issue)

Source: Internet
Author: User

Post Office
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 17110 Accepted: 9226

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 51 2 3 6 7 9 11 22 44 50

Sample Output

9

Source

IOI 2000


Is the post office problem.

For the establishment of M post offices in the village of 1~n, it is required that all villages be the smallest distance from the post office.

Many post offices are difficult to understand, you can see a post office first. In the 1~n, build a post office, can be built in the middle, must be to ensure the minimum total distance. If you build M, then you can think: 1~k village has been built between the m-1, and then in the k~n between villages as long as the building of one is enough. and the shortest distance to build this post office is available. As a result, we can break the problem down into sub-problems.

For such a problem, you can set up a dp[i][j], indicating the minimum total distance of J Post offices in the village of 1~i.

So we can get the state transfer equation: Dp[i][j]=max (dp[i][j],dp[k] [j-1]+sum[k+1] [j])

Where Sum[i][j] is the minimum distance from the village I to J to establish a post office. Can be obtained: sum[I [J]=sum[i][j-1]+x[j]-x[(I+J)/2];

Then you can get the DP boundary: Dp[i][1]=sum[1][i];

#include <stdio.h> #include <string.h>int min (int a,int b) {if (a<b) return a;else return B; int main () {int v,p,i,j,k,l,x[305],dp[305][305],sum[305][305];while (scanf ("%d%d", &v,&p)!=eof) {memset (sum, 0,sizeof (sum)); for (i=0;i<305;i++) for (j=0;j<305;j++) dp[i][j]=10000000;for (i=1;i<=v;i++) {scanf ("%d", &x[i]); sum[i][i]=0;} 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][1]=sum[1][i];for (j=1;j<=p;j++) {for (i=j+1;i<=v;i++) {for (k=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;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 1160 Post Office (Post office issue)

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.