POJ 1160: Post Office classic DP

Source: Internet
Author: User

POJ 1160: Post Office classic DP

 

Post Office
Time Limit:1000 MS   Memory Limit:10000 K
Total Submissions:17168   Accepted:9270

 

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
In these villages, P is selected to build post offices. It is required that all the villages have the shortest distance from the post office. The shortest distance.

For the Post Office's classic DP. If it is violent, it must be TLE, so we can only think about it on DP.

Then I thought from an early age that if I had set up a post office from a to B (in the middle of B-a + 1 village, it must have been built in the middle of the village. 1 and 3 should be set to 2. 1 and 4 can be set to 2 or 3 because the distance is the same.

Then, when doing this rule, I also discovered:

Sum [I] [j] indicates the distance from village I to Village j to establish a post office. Then sum [I] [j] = sum [I] [J-1] + value [j]-value [(I + j)/2]

You see, sum [1] [4] (created in 2 or 3) = (value [4]-value [3]) + (value [3]-value [2]) + (value [3]-value [1])

= Value [4] + value [3]-value [2]-value [1]

Sum [1] [5] (created in 3) = (value [5]-value [3]) + (value [4]-value [3]) + (value [3]-value [2]) + (value [3]-value [1])

= Value [5] + value [4]-value [2]-value [1]

Through this we can find this law: sum [I] [j] = sum [I] [J-1] + value [j]-value [(I + j)/2]

 

In this way, we split the issue of building P post offices in 1 to V villages, and learned the optimal solution for building a post office in a certain part, followed by dp derivation.

Dp [I] [j] indicates the optimal solution for building the I post office from 1 to j villages, dp [I] [j] = min (dp [I] [j], dp [I-1] [k-1] + sum [k] [j]);

The formula above indicates that the establishment of I post office in 1 to j villages is divided into two parts, one part is the optimal solution of the establishment of K-1 post office in 1 to I-1 village, the other part is to create an optimal solution for the Post Office from k to j, and constantly enumerate k values from 1 to j. The minimum value is the optimal solution.

There was a problem with this question. At that time, I couldn't figure out how to determine the starting value. Now, in summary, it's really pig's head. The initial value is not dp [1] [I] = sum [1] [I. At that time, I had no idea at all...

Code:

 

# Include
 
  
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Pragma warning (disable: 4996) using namespace std; int V, P; int value [305]; int sum [305] [305]; int dp [35] [1, 305]; int main () {int I, j, k; memset (sum, 0, sizeof (sum); for (I = 0; I <= 30; I ++) {for (j = 0; I <= 300; j ++) {dp [I] [j] = 10000 ;}} cin> V> P; value [0] = 0; for (I = 1; I <= V; I ++) {cin> value [I];} sum [1] [2] = value [2]-value [1]; for (I = 1; I <= V; I ++) {for (j = I + 1; j <= V; j ++) {sum [I] [j] = sum [I] [J-1] + value [j]-value [(I + j)/2];} for (I = 1; I <= V; I ++ )//!!!! Start value {dp [1] [I] = sum [1] [I] ;}for (I = 2; I <= P; I ++) {for (j = I; j <= V; j ++) {for (k = 1; k <= j; k ++) {dp [I] [j] = min (dp [I] [j], dp [I-1] [k-1] + sum [k] [j]);} cout <
      
       

 

 

 

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.