Xtu summer individual 5 F-Post Office

Source: Internet
Author: User
Post officetime limit: 1000 msmemory limit: 10000 kbthis problem will be judged on PKU. Original ID: 1160
64-bit integer Io format: % LLD Java class name: Main 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.
Inputyour 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. outputthe 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
Sourceioi 2000: DP, DP [I] [J] indicates that I post office is responsible for the shortest distance and of J villages. What is the distance and minimum? It must be the selected point. If the point (1 <= 1 <= n/2) is in the center of the nth and Nth-I villages, the distance will be the smallest!
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #define LL long long10 #define INF 0x3f3f3f3f11 using namespace std;12 int dp[35][301],x[301],n,m;13 int dis(int i,int j){14     int sum = 0;15     while(i < j) sum += x[j--]-x[i++];16     return sum;17 }18 int main(){19     int i,j,k;20     while(~scanf("%d%d",&m,&n)){21         memset(dp,0,sizeof(dp));22         for(i = 1; i <= m; i++){23             scanf("%d",x+i);24             dp[1][i] = dis(1,i);25         }26         for(i = 2; i <= n; i++){27             for(j = i; j <= m; j++){28                 dp[i][j] = INF;29                 for(k = i-1; k < j; k++)30                     dp[i][j] = min(dp[i][j],dp[i-1][k]+dis(k+1,j));31             }32         }33         cout<<dp[n][m]<<endl;34     }35     return 0;36 }
View code

 

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.