[STL][multiset] hdu4268 Alice and Bob

Alice and Bob

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

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
2012 ACM/ICPC Asia Regional Changchun Online

题意:Alice 和 Bob 分别有 n 张牌,每张牌有一个长(h)和一个宽(w),Bob把他的牌依次排一排,Alice用他的牌依次去覆盖,求最多可以 覆盖多少,覆盖的条件是长和宽都要大于等于Bob的牌。
分析:对Alice 和Bob的牌都按 h 排序,然后对Alice的每张牌,在Bob的牌中找一个最大能覆盖的 。也就是说首先满足Alice的牌 h >= Bob的牌 h,然后找最大 w。在查找最大的w时,可以使用multiset中的lower_bound。multiset动态插入每次只插入满足Alice的牌 h >= Bob的牌。

多么痛的领悟。。STL是多么地重要啊。。

#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
const int MAXN = 100010;

struct node{
    int w,h;
}A[MAXN],B[MAXN];

bool cmp(node a,node b)
{
    return a.wmultiset<int> myset;

int main()
{
    int t,n,i,j,ans;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(i=0;iscanf("%d%d",&A[i].w,&A[i].h);
        for(i=0;iscanf("%d%d",&B[i].w,&B[i].h);
        sort(A,A+n,cmp);
        sort(B,B+n,cmp);
        myset.clear();
        ans=0;
        multiset<int>::iterator it;
        for(i=0,j=0;iwhile(j=B[j].w)
            {
                myset.insert(B[j].h);
                j++;
            }
            if(myset.empty())continue;
            it=myset.lower_bound(A[i].h);
            if(it==myset.end()||A[i].h<*it) it--;
            if(*it<=A[i].h)
            {
                ans++;
                myset.erase(it);
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(1,数据结构,————ACM训练————)