Poj1160 Post Office

Source: Internet
Author: User

Poj1160 Post Office

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

This is a classic topic of interval dp. I wrote it myself after reading other people's code. You can set two arrays, sum [I] [j] and dp [I] [j], sum [I] [j] indicates that a post office is built between the village I and the Village j, and the minimum cost calculated by the post office is the same as that calculated by the post office, dp [I] [j] indicates that j post offices are in the middle of the first village, and the cost calculation of these villages is the minimum cost associated with these post offices.

Then the minimum cost of building j post offices in the first village can be the minimum cost of building a post office in the first k villages in I-1 plus the minimum cost of building a post office from the k Post Office to the I Post Office cost transferred, dp [I] [j] = min (dp [I] [j], dp [k] [J-1] + sum [k + 1] [I]);

The initial condition here is dp [I] [1] = sum [1] [I].

When the number of post offices is 1, we can just build them in the middle. When there are multiple post offices, sum [I] [j] = sum [I] [J-1] + pos [j]-pos [(I + j)/2].

 

#include
 
  #include
  
   #define maxn 350#define inf 88888888int min(int a,int b){return a
   
    

 

 

Related Article

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.