hdu4268 multiset應用 貪心

life4711發表於2015-03-21

http://acm.hdu.edu.cn/showproblem.php?pid=4268

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
/**
hdu 4268  multiset應用
題目大意:一牌,如果他的長和寬都分別不小於另一張,那麼這張牌就可以覆蓋另一張,問在A的牌中,有多少
          可以覆蓋B的牌(一張牌只能覆蓋一張,並且長寬已給出,並不一定長度大的的就是長)
解題思路:這道題的資料如果小一點,完全可以用二分圖來做。對AB的牌進行排序長遞增,然後列舉A的牌,把所有B
          的牌大於A的長的加入multiset裡,利用lower_bound()找出寬最小的一個,然後刪除該元素。複雜度O(n*logn)
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
const int maxn=100005;

int n;
multiset <int> s;

struct note
{
    int x,y;
    bool operator < (const note &other)const
    {
        return x<other.x;
    }
}a[maxn],b[maxn];

int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&a[i].x,&a[i].y);
        }
        for(int i=0;i<n;i++)
        {
            scanf("%d%d",&b[i].x,&b[i].y);
        }
        sort(a,a+n);
        sort(b,b+n);
        s.clear();
        int k=0,ans=0;
        for(int i=0;i<n;i++)
        {
            while(a[i].x>=b[k].x&&k<n)
            {
                 s.insert(b[k].y);
                 k++;
            }
            if(s.empty())continue;
            multiset<int>::iterator it=s.lower_bound(a[i].y);
            if(it==s.end()||a[i].y<*it)it--;
            if(*it<=a[i].y)
            {
                ans++;
                s.erase(it);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}


相關文章