首页 > 其他 > 详细

BZOJ1108 [POI2007]天然气管道Gaz

时间:2016-01-22 14:06:16      阅读:148      评论:0      收藏:0      [点我收藏+]

题意:有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;
}

BZOJ1108 [POI2007]天然气管道Gaz

原文:http://www.cnblogs.com/lcchuguo/p/5150528.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!