Alice and Bob(mutiset容器)

Alice and Bob

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

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
 

题解:

Alice 用自己的牌覆盖Bob的牌,问最多可以覆盖多少张;用mutiset容器,multiset可以存多个同值的点;另外在外边写二分会超时,mutiset里面的二分不会超时。。。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<algorithm>
using namespace std;
const int MAXN = 100100;
multiset<int>st;
struct Node{
    int x, y;
    friend bool operator < (Node a, Node b){
        if(a.x != b.x)
            return a.x < b.x;
        else 
            return a.y < b.y;
    }
    void input(){
        scanf("%d%d", &this->x, &this->y);
    }
};
Node Alice[MAXN], Bob[MAXN];
int main(){
    int T, N;
    scanf("%d", &T);
    while(T--){
        scanf("%d", &N);
        for(int i = 0; i < N; i++){
            Alice[i].input();
        }
        sort(Alice, Alice + N);
        for(int i = 0; i < N; i++){
            Bob[i].input();
        }
        sort(Bob, Bob + N);
        int ans = 0;
        st.clear();
        multiset<int>::iterator iter;
        for(int i = 0, j = 0; i < N; i++){
            while(j < N && Alice[i].x >= Bob[j].x){
                st.insert(Bob[j].y);
                j++;
            }
            if(st.empty())continue;
            iter = st.lower_bound(Alice[i].y);
            if(iter == st.end() || *iter > Alice[i].y){
                iter--;
            }
            if(Alice[i].y >= *iter)ans++, st.erase(iter);
        }
        printf("%d
", ans);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/handsomecui/p/5440584.html