AtCoder Regular Contest 092 C - 2D Plane 2N Points 贪心 匈牙利算法模板

Problem Statement
On a two-dimensional plane, there are N red points and N blue points. The coordinates of the i-th red point are (ai,bi), and the coordinates of the i-th blue point are (ci,di).


A red point and a blue point can form a friendly pair when, the x-coordinate of the red point is smaller than that of the blue point, and the y-coordinate of the red point is also smaller than that of the blue point.


At most how many friendly pairs can you form? Note that a point cannot belong to multiple pairs.


Constraints
All input values are integers.
1≤N≤100
0≤ai,bi,ci,di<2N
a1,a2,…,aN,c1,c2,…,cN are all different.
b1,b2,…,bN,d1,d2,…,dN are all different.
Input
Input is given from Standard Input in the following format:


N
a1 b1
a2 b2
:
aN bN
c1 d1
c2 d2
:
cN dN
Output
Print the maximum number of friendly pairs.


Sample Input 1

3
2 0
3 1
1 3
4 2
0 4
5 5
Sample Output 1

2
For example, you can pair (2,0) and (4,2), then (3,1) and (5,5).


Sample Input 2

3
0 0
1 1
5 2
2 3
3 4
4 5
Sample Output 2
2
For example, you can pair (0,0) and (2,3), then (1,1) and (3,4).


Sample Input 3

2
2 2
3 3
0 0
1 1
Sample Output 3

0
It is possible that no pair can be formed.


Sample Input 4

5
0 0
7 3
2 2
4 8
1 6
8 5
6 9
5 4
9 1
3 7
Sample Output 4

5
Sample Input 5

5
0 0
1 1
5 5
6 6
7 7
2 2
3 3
4 4
8 8
9 9
Sample Output 5
Copy

4

题意:给出n个红色坐标,n个蓝色坐标,如果某一个蓝色坐标大于某一个红色坐标,则构成一对坐标对,每个坐标只可以用一次

,求最多有多少坐标对

思路:

1.贪心:用符合条件最小的q去匹配符合条件最大的p 

类似题目:

校赛 搬书  http://blog.csdn.net/deepseazbw/article/details/78836942

HDU - 1051 Wooden Sticks http://blog.csdn.net/deepseazbw/article/details/78820869

#include <bits/stdc++.h>
using namespace std;
const int N = 205;
typedef long long ll;
struct node
{
    int a;
    int b;
} p[N],q[N];
int vis[N];
bool cmp(node x,node y)
{
    if(x.a!=y.a)
        return x.a<y.a;
    else
        return x.b<y.b;
}

int main()
{
    int n;
    cin>>n;
    for(int i=1; i<=n; i++)
        scanf("%d%d",&p[i].a,&p[i].b);
    for(int i=1; i<=n; i++)
        scanf("%d%d",&q[i].a,&q[i].b);
    memset(vis,0,sizeof(vis));
    sort(p+1,p+n+1,cmp);
    sort(q+1,q+n+1,cmp);
    int cnt=0;
    //贪心用符合条件最小的q去装符合条件最大的p
    for(int j=1; j<=n; j++)
    {
        int mid=-1,flag=0;
        for(int i=1; i<=n; i++)
        {
            if(!vis[i]&&p[i].a<q[j].a&&p[i].b<q[j].b)   //先保证存在
            {
                flag++;
                if(flag==1)
                    mid=i;
                else if(p[i].b>p[mid].b)
                    mid=i;
            }
        }
        if(mid>0)
        {
            vis[mid]=1;
            cnt++;
        }
    }
    printf("%d\n",cnt);
    return 0;
}

2.暴力枚举 DFS 超时 列举出所有的情况

#include <bits/stdc++.h>
using namespace std;
const int N = 205;
typedef long long ll;
struct node
{
    int a;
    int b;
} p[N],q[N];
int vis[N];
int sum,n,ans;
bool cmp(node x,node y)
{
    if(x.a!=y.a)
        return x.a<y.a;
    else
        return x.b<y.b;
}
void dfs(int qbegin,int ans)
{
    if(qbegin>n)
    {
        sum=max(sum,ans);
        return ;
    }
    for(int j=qbegin; j<=n; j++)
    {
        for(int i=1; i<=n; i++)
        {
            if(q[j].a>p[i].a&&q[j].b>p[i].b&&!vis[i])
            {
                ans++;
                vis[i]=1;
                dfs(j+1,ans);
                vis[i]=0;
                ans--;
            }
        }
    }
}
int main()
{

    while(cin>>n)
    {
        for(int i=1; i<=n; i++)
            scanf("%d%d",&p[i].a,&p[i].b);
        for(int i=1; i<=n; i++)
            scanf("%d%d",&q[i].a,&q[i].b);
        memset(vis,0,sizeof(vis));
        sort(p+1,p+n+1,cmp);
        sort(q+1,q+n+1,cmp);
        sum=0;
        dfs(1,0);
        printf("%d\n",sum);
    }
    return 0;
}

3.匈牙利算法的模板题目

参考博客

http://blog.csdn.net/cillyb/article/details/55511666

http://blog.csdn.net/qq_35935435/article/details/54906008

#include <bits/stdc++.h>
#include <string>
using namespace std;

typedef long long ll;
const int maxn = 200005;
const ll mod = 1e9+7;
const int MAX_int = 1e9;
const double eps = 1e-9;
int vis[105];
int relation[105][105];
int match[105];
int n;
struct node
{
    int a,b;
} p[105],q[105];

bool find(int x)
{
    for (int j=1; j<=n; j++)
    {
        if (relation[x][j]==1 && vis[j]==0)
        {
            vis[j]=1;
            if (match[j]==0 || find(match[j]))
            {
                //名花无主或者能腾出个位置来,这里使用递归
                match[j]=x;
                return true;
            }
        }
    }
    return false;
}

int main()
{
    memset(match,0,sizeof(match));
    memset(relation,0,sizeof(relation));
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
        scanf("%d%d",&p[i].a,&p[i].b);
    for(int i=1; i<=n; i++)
        scanf("%d%d",&q[i].a,&q[i].b);
    for(int i=1; i<=n; i++)
        for(int j=1; j<=n; j++)
            if(p[i].a <q[j].a && p[i].b <q[j].b)
                relation[i][j]=1;

    int cnt=0;
    for (int i=1; i<=n; i++)
    {
        memset(vis,0,sizeof(vis));    //这个在每一步中清空
        if (find(i))
            cnt++;
        }
    printf("%d\n",cnt);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值