hdu4268 Alice and Bob

Alice and Bob

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1800 Accepted Submission(s): 645


Problem Description
Alice and Bob's game never ends. Today, they introduce a new game. In this game, both of them have N different rectangular cards respectively. Alice wants to use his cards to cover Bob's. The card A can cover the card B if the height of A is not smaller than B and the width of A is not smaller than B. As the best programmer, you are asked to compute the maximal number of Bob's cards that Alice can cover.
Please pay attention that each card can be used only once and the cards cannot be rotated.

Input
The first line of the input is a number T (T <= 40) which means the number of test cases.
For each case, the first line is a number N which means the number of cards that Alice and Bob have respectively. Each of the following N (N <= 100,000) lines contains two integers h (h <= 1,000,000,000) and w (w <= 1,000,000,000) which means the height and width of Alice's card, then the following N lines means that of Bob's.

Output
For each test case, output an answer using one line which contains just one number.

Sample Input
  
  
2 2 1 2 3 4 2 3 4 5 3 2 3 5 7 6 8 4 1 2 5 3 4

Sample Output
  
  
1 2
唉,这题不说了,打比赛,花了五个小时也没做出来,打队友给坑了,下来后思考了很长时间,才发现,我思路有问题,这里,因为,不是每两个牌都有确定的大小关系,因为
有可以是一个宽度大,而另一个是长度大,这样一大一小的情况,让人伤脑筋,其实,可以这样,我们先按长,再按宽排,这样排安序之后,我们用一个set把所有的第二个人的牌存进,存入的是宽,因为,我们是先按长度从小到大的,这样我们就能保证,在前面的一定,是长小的,我们再用一个upper_bound找出第一个比这个数的宽要大的—,再向前移一位,那么就找到了一个最大的能盖住的长方形,注意,一定是要最大的,如果不是最大的,在最后就可可能后面的取不到的情况,这也就是一个贪心的做法!好了,到这里,这题就可以a了!注意要用一个set啊什么的,加快查询的速才行!
#include<iostream>
#include <stdio.h>
#include <algorithm>
#include <set>
using namespace std;
struct node {
    __int64 h,w;
    int n;
}l[200005];
multiset<int >myset;
bool cmp(node a,node b)
{
    if(a.h!=b.h)
    return a.h<b.h;
    else{
        if(a.w!=b.w)
        return a.w<b.w;
        else
        {
            return a.n<b.n;
        }
    }
}
int main ()
{
    int tcase,n2,i,re,n;
    multiset<int >::iterator p;
    scanf("%d",&tcase);
    while(tcase--)
    {
        myset.clear();
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            scanf("%I64d%I64d",&l[i].h,&l[i].w);
            l[i].n=1;
        }
        n2=2*n;
        for(;i<n2;i++)
        {
           scanf("%I64d%I64d",&l[i].h,&l[i].w);
           l[i].n=0;
        }
        sort(l,l+n2,cmp);
        re=0;
        for(i=0;i<n2;i++)
        {
            if(!l[i].n)
            {
                myset.insert(l[i].w);
            }
            else{
                if(!myset.empty()&&*myset.begin()<=l[i].w)
                {
                    p=myset.upper_bound(l[i].w);
                    p--;//找到小一个或相等的
                    re++;
                    myset.erase(p);

                }

            }
        }
        printf("%d\n",re);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值