[POJ 1160] Post Office【DP+四边形不等式优化】

  • 2018-01-25
  • 0
  • 1

Problem:

Time Limit: 1000MS Memory Limit: 10000K

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

Solution:

这是 IOI 的一道经典 DP 题。

令 dp[i][j] 为前 i 个村庄建 j 个邮局,所能达到的最小距离总和,cost[i][j] 表示在第 i ~ j 个村庄中建一个邮局,所增加的最小距离总和,则

  • cost[ i ][ i ] = 0 …… (1)
  • cost[ i ][ j ] = cost[ i ][ j - 1 ] + X[ j ] - X[ ( i + j ) / 2 ], 其中 i < j ≤ V …… (2)
  • dp[ i ][ j ] = min{ dp[ k ][ j - 1] + cost[ k + 1 ][ i ] }, 其中 1 ≤ k < i …… (3)

证明如下:

  • (1) 式显然正确,(2) 式是由于邮局建在中位数处距离总和最小,可以画图证明。
  • (3) 式的正确性并不显然,主要疑点在于转移时能否保证离前 k 个村庄最近的邮局均不变。
  • 我们很容易发现,dp[ i ][ j ]  ≤ dp[ i ][ j - 1 ]
  • 若转移时第 p ~ k 个村庄 (1 ≤ p ≤ k) 反而离新建的邮局更近,那么
    • dp[ p ][ j - 1 ] + cost[ p + 1 ][ i ] < dp[ k ][ j - 1 ] + cost[ p + 1 ][ i ]
    • 所以此时不可能更新 dp 而导致答案错误。

该算法的时间复杂度为 O(V3P),见 Code #1,理论上会 TLE,但是数据太水还是过了。。

然后我们发现这个 DP 可以用平行四边形优化

证明起来太复杂,我们可以从结论出发,记录每次的最优决策点 s,直接输出 s[i][j - 1], s[i][j], s[i + 1][j],观察是否满足 s[i][j - 1] ≤ s[i][j] ≤ s[i + 1][j] 即可 ^_^

然后我们发现除了边界条件之外都满足上式,所以只要处理一下边界就可以改进 DP 方程了:

  • dp[ i ][ j ] = min{ dp[ k ][ j - 1] + cost[ k + 1 ][ i ] }, 其中 s[i][j - 1] ≤ k ≤ s[i + 1][j]

这样时间复杂度就降到了 O(V2P),见 Code #2,优化效果还是很明显的。

Code #1: O(V3P) [1064K, 16MS]

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

int V, P, X[310];
int dp[310][30], cost[310][310];

int main(){
	scanf("%d%d", &V, &P);
	for(register int i = 1; i <= V; i++) scanf("%d", X + i);
	for(register int i = 1; i <= V; i++){
		cost[i][i] = 0;
		for(register int j = i + 1; j <= V; j++)
			cost[i][j] = cost[i][j - 1] + X[j] - X[i + j >> 1];
	}
	memset(dp, 0x3f, sizeof(dp)), dp[0][0] = 0;
	for(register int j = 1; j <= P; j++)
		for(register int i = j; i <= V; i++)
			for(register int k = 0; k < i; k++)
				dp[i][j] = min(dp[i][j], dp[k][j - 1] + cost[k + 1][i]);
	printf("%d\n", dp[V][P]);
	return 0;
}

Code #2: O(V2P) [1112K, 0MS]

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

int V, P, X[310];
int dp[310][35], cost[310][310];
int s[310][35];

int main(){
	scanf("%d%d", &V, &P);
	for(register int i = 1; i <= V; i++) scanf("%d", X + i);
	for(register int i = 1; i <= V; i++){
		cost[i][i] = 0;
		for(register int j = i + 1; j <= V; j++)
			cost[i][j] = cost[i][j - 1] + X[j] - X[i + j >> 1];
	}
	memset(dp, 0x3f, sizeof(dp));
	for(register int i = 1; i <= V; i++){
		dp[i][1] = cost[1][i];
		s[i][1] = 1;
	}
	for(register int j = 1; j <= P; j++) s[V + 1][j] = V - 1;
	for(register int j = 2; j <= P; j++)
		for(register int i = V; i >= j; i--)  // Reversed order
			for(register int k = s[i][j - 1]; k <= s[i + 1][j]; k++)
				if(dp[k][j - 1] + cost[k + 1][i] < dp[i][j]){
					dp[i][j] = dp[k][j - 1] + cost[k + 1][i];
					s[i][j] = k;
				}
	printf("%d\n", dp[V][P]);
	return 0;
}

评论

  • bjz回复

    %%%%%%%%%%%%%



新博客地址:
darkleafin.cf
(该域名已过期且被抢注。。)
darkleafin.github.io


常年不在线的QQ:
49750

不定期更新的GitHub:
https://github.com/Darkleafin


OPEN AT 2017.12.10

如遇到代码不能正常显示的情况,请刷新页面。
Please refresh the page if the code cannot be displayed normally.


发现一个优美的网站:
https://visualgo.net/en
















- Theme by Qzhai