码迷,mamicode.com
首页 > 其他好文 > 详细

hdu4268 multiset应用 贪心

时间:2015-03-21 11:17:51      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

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;
}


hdu4268 multiset应用 贪心

标签:

原文地址:http://blog.csdn.net/lvshubao1314/article/details/44514205

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!