HDU 4268-Alice and Bob-贪心

典型的双关键字排序咯。。


A,B分别排好序

从大到小遍历B,每次遍历到Bi时,确保 所有第一关键字大于Bi的 Aj都要在一个set里候选,即可保证贪心正确


#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>


using namespace std;
const int N=21234;
struct node
{
    int x,y;
    node() {}
    node(int a,int b)
    {
        x=a,y=b;
    }
    bool operator <(const node b)const
    {
        return y<b.y;
    }
};
node A[112345];
node B[112345];
set<node > sb;
set<node > ::iterator it;
bool cmp(node a,node b)
{
    if (a.x!=b.x)
        return a.x<b.x;
    else
        return a.y<b.y;
}
int  main()
{

    int t;
    cin>>t;
    while(t--)
    {
        int n;
        scanf("%d",&n);
        int x,y;
        for (int i=1; i<=n; i++)
        {
            scanf("%d%d",&x,&y);
            A[i]=node(x,y);
        }
        for (int i=1; i<=n; i++)
        {
            scanf("%d%d",&x,&y);
            B[i]=node(x,y);
        }
        sb.clear();
        sort(A+1,A+1+n,cmp);
        sort(B+1,B+1+n,cmp);
        int j=n;
        int ans=0;
        for (int i=n; i>=1; i--)
        {
            while(A[j].x>=B[i].x&&j>0)
            {
                sb.insert(A[j]);
                j--;
            }
            it=sb.lower_bound(B[i]);
            if (it!=sb.end())
            {
                ans++;
                sb.erase(it);
            }
        }
        printf("%d\n",ans);
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值