POJ1160 "Post Office"

Source: Internet
Author: User

POJ1160 "Post Office" 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

Solution:

DP
Status: F[i][j]: The first I village, put J a minimum sum value of the Post office
Output: F[n][m]
Transfer equation: F[i][j]=min{f[k][j-1]+sum[k+1][i]}
Explain: It is clear that the state of the former I village was transferred from the previous state, but we are unsure of the location of the post office and whether there is any, we need to enumerate the state of the former K village (where 1<=k<=i), and the former K village must have j-1 post office, plus from K+1~i built minimum sum value for a post office
SUM[I][J]: Represents the minimum sum value of a post office built from I->j.
If there is only one post office in I->j and no other village it is clear that the minimum value is pos[j]-pos[i]; if there are two villages in the middle, we will build in these two villages, so that infinite reasoning goes on, and finally a post office is built into a marker median value.
sum[i][j]=sum[i][j-1]+pos[j]-pos[(I+J)/2];
Note :

    1. F[i][1]=sum[1][i];
    2. The initial value of each F[I]][J] is appended to the INF
    3. J is enumerated before I, and j>=2

Code:

#include <stdio.h>#include <string.h>#define MAXN#define INF 999999int N,m;int a[MAXN],sum[MAXN][MAXN],f[MAXN][MAXN];int min (int a,int b) {return a<b?a:b;} int Main(){scanf("%D%D",&N,&m); for(int I=1;i<=n;i++){scanf("%D",&a[I]); } for(int I=1;i<n;i++){ for(int J=i+1;j<=n;j++){sum[I][J]=sum[i][j-1]+a[j]-a[(i+j)/2]; }    } for(int I=1;i<=n;i++){F[I][1]=Sum[1][i];} for(int J=2;j<=m;j++){ for(int I=j+1;i<=n;i++){F[I][J]=INF;             for(int k=1;k<i;k++){F[I][J]=min (f[i][j],f[k][j-1]+sum[k+1][i]);}        }    }printf("%D\N",F[N][m]);return 0;}

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

POJ1160 "Post Office"

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.