HDU 4268 Alice and Bob 第37届ACM/ICPC长春赛区网络赛1002题 (贪心+multiset)

Alice and Bob

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


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
 
Source
 
Recommend
liuyiding
 
 
长春赛第二题。。。比赛的时候没有想到贪心的方法。。。否则用STL中的multiset可以快速A掉的。膜拜twinkle大神自己写平衡二叉树来把这题A掉了!
厉害!
multiset其实内部就是用平衡二叉树来实现的。
 
贪心方法就是找h可以覆盖的条件下找w最大的去覆盖。
/*
HDU 4268
贪心+STL
*/

#include<stdio.h>
#include<math.h>
#include<iostream>
#include<set>
#include<algorithm>
using namespace std;
const int MAXN=200010;
struct Node
{
    int h,w;
    int flag;
}node[MAXN];
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.flag>b.flag;
}
multiset<int>mt;
multiset<int>::iterator it;
int main()
{
   // freopen("in.txt","r",stdin);
   // freopen("out.txt","w",stdout);
    int T;
    int n;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&node[i].h,&node[i].w);
            node[i].flag=1;
        }
        for(int i=n+1;i<=2*n;i++)
        {
            scanf("%d%d",&node[i].h,&node[i].w);
            node[i].flag=2;
        }
        sort(node+1,node+2*n+1,cmp);
        mt.clear();
        int ans=0;
        for(int i=1;i<=2*n;i++)
        {
            if(node[i].flag==2)mt.insert(node[i].w);
            else
            {
                if(!mt.empty())
                {
                    it=mt.begin();
                    if(node[i].w>=(*it))
                    {
                        ans++;
                        it=mt.upper_bound(node[i].w);
                        it--;
                        mt.erase(it);
                    }
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/kuangbin/p/2679227.html