BZOJ1108 [POI2007]天然气管道Gaz

来源:互联网 发布:二维数组的定义与应用 编辑:程序博客网 时间:2024/04/25 00:43

题意:有N个黑点和N个白点,每个黑点只能水平向右和竖直向下延伸,与一个白点配对。权值为两个点之间的曼哈顿距离。求使得n对点均配对最小的配对总权值。


思路:假设存在一组解,那么我们交换一对配对,若依旧能够满足条件,画画图就知道答案不变。

因此对于任意可行配对答案均相同。

于是我们统计出所有黑点的-x+y,所有白点的x-y,累加起来即可。


Code:

#include <cstdio>#include <cstring> typedef long long LL;int main() {    int n;    scanf("%d", &n);    long long res = 0;    int x, y;    register int i;    for(i = 1; i <= n; ++i)        scanf("%d%d",&x, &y), res += y-x;    for(i = 1; i <= n; ++i)        scanf("%d%d", &x, &y), res -=y-x;    printf("%lld", res);    return 0;}

0 0
原创粉丝点击