Punching Power Gym - 101670J (二分图匹配)

Punching Power Gym - 101670J (二分图匹配)

Problem Description

The park management finally decided to install some popular boxing machines at various strategic places in the park. In fact, to compensate for the previous lack of machines, they decided to install as many machines as possible. Surprisingly enough, the park is not going to be choked with new machines because there are some quite serious legal limitations regarding the locations of the machines. The management has marked all possible boxing machine locations and their respective coordinates on the park plan. Additionally, they also have to respect manufacturer security rule: The distance between any two boxing machines has to be at least 1.3 meters.
Help the management to establish the maximum possible number of boxing machines which can be installed in the park.

Input Specification

There are several test cases. Each case starts with a line containing one integer N which specifies the number of possible boxing machine locations in the park (1 ≤ N ≤ 2000). Next, there are N lines representing the location coordinates, each line describes one location by a pair of integer coordinates in meters. All locations in one test case are unique. Each coordinate is non-negative
and less than or equal to 1e9.
You are guaranteed that all locations form a single connected group, that is, it is possible to start in any location and reach any other location by a sequence of steps, each of which changes exactly one coordinate by 1, without leaving the area suitable for placing boxing machines.

Output Specification

For each test case, print a single line with one integer representing the maximum number of boxing machines which can be installed in the park.

Sample Input

4
0 0
0 1
1 0
1 1
6
0 1
1 0
1 1
1 2
1 3
2 2

Output for Sample Input

2
4

题意

问N个点中,最多可以有多少个点,它们任意两个点的距离>=1.3
多组输入N
接下来N行,每行两个整数X,Y,代表点的坐标
(1<=N<=2000)(1<=X,Y<=1e9)

思路

点的个数不多。关键是要求的距离>=1.3,所以只有对于两个点上下或者左右相邻的才会是距离为1,即<=1.3(因为点的坐标为正整数)
可以这样看,对于点的横纵坐标和都为奇数的点肯定符合题意,因为差值为2,所以任意两个点距离至少为根号2(1,2)、(2,3),或者2
同理对于横纵坐标和为偶数的点,肯定也符合题意
所以可以任选对于坐标和为奇数或者偶数的所有点来个二分图匹配,这样N-匹配数,就是符合条件的点
可以这样看答案:
坐标和为奇数点的个数+为偶数的个数-为偶数的点中可以与为奇数匹配的最大个数

同理

坐标和为偶数点的个数+为奇数的个数-为奇数的点中可以与为偶数匹配的最大个数

上面两种都是答案,本质一样

AC代码

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
#define maxn 2010
typedef struct node{  //存点的横纵坐标
    int x;
    int y;
}node;
node a[maxn];
bool cmp(node p,node q)  //按照横纵坐标和排序,其实不用排序
{
    return (p.x+p.y)<(q.x+q.y);
}
vector<int>e[maxn];  //存距离为1的点
int n,link[maxn];  
bool vis[maxn];  //标记是否访问
bool dfs(int u)
{
    int i,k;
    for(i=0;i<e[u].size();i++){
        k=e[u][i];
        if(!vis[k]){
            vis[k]=true;
            if(link[k]==-1||dfs(link[k])){
                link[k]=u;
                return true;
            }
        }
    }
    return false;
}
int main()
{
    while(scanf("%d",&n)!=EOF){
        int i,j;
        for(i=1;i<=n;i++){
            scanf("%d %d",&a[i].x,&a[i].y);
            e[i].clear();
        }
        //sort(a+1,a+1+n,cmp);  
        for(i=1;i<=n;i++){
            for(j=1;j<=n;j++){
                if((abs(a[i].x-a[j].x)+abs(a[i].y-a[j].y))==1){  
                    e[i].push_back(j);  //把距离为1的点加入vector
                    e[j].push_back(i);
                }
            }
        }
        memset(link,-1,sizeof(link));
        int ans=0;
        for(i=1;i<=n;i++){
            if((a[i].x+a[i].y)%2==1){
                memset(vis,0,sizeof(vis));
                if(dfs(i)){
                    ans++;
                }
            }
        }
        printf("%d\n",n-ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值