1160 Post Office//사각형 최적화 없음

Post Office
Time Limit: 1000MS
 
Memory Limit: 10000K
Total Submissions: 10035
 
Accepted: 5397
Description
There is a straight highway with villages 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 villages 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 villages. A village and the post office in it have the same position. For building the post offices, their positions should 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 villages 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 villages 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 villages. 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 5
1 2 3 6 7 9 11 22 44 50

Sample Output
9

Source
IOI 2000
/*전 v개 마을에서 전 p개 우체국을 건설할 때의 최소 거리를 m[v,p]로 설정합니다.처음에 0-1 가방의 사고방식에 따라 m[v,p]=min{m[v-1,p], m[v-1,p-1]+dis_cost} 공식은 이해하기 쉬우나 계산하기 어렵다.따라서 우리는 위의 공식에 따라 m[v,p] 행렬을 초기화할 수 있다.p>1의 상황을 고려하여 만약에 m[v,p-1]을 알고 있다면 v개 마을에 p-1개의 우체국을 건설했고 만약에 우체국을 재건한다면 m[v,p]를 구하는 것이 훨씬 낫다.v개 마을에 우체국을 하나 더 짓는 것을 고려하면 r~v,(r=p-1)을 두루 돌아다니며 최소치를 찾을 수 있다.따라서 아래의 추이 관계식을 얻습니다: m[v,p]=min{m[i,p-1]+dis(i+1,v),i=p-1,...v-1}*/#include#include#define min(a,b)a>b?b:a; int dp[31][301],sum[301][301]; int p[301]; int n,v; intmain() {scanf('%d%d', &n, &v); for(int i=1;i<=n;i++)scanf('%d', &p[i]); for(int i=1;i+) for(int j=i+1;j

좋은 웹페이지 즐겨찾기