zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Linux入门(四)linux运行环境mysql详细操作及安装phpmyadmin
    Linux入门(五)linux服务器文件远程管理
    Linux入门(六)ubuntu下vim编辑器安装与使用
    windows下9款一键快速搭建PHP本地运行环境的好工具(含php7.0环境)
    Linux入门(三)搭建服务器linux运行环境LAMP/LNMP
    Linux入门(二)Linux基本命令及基本操作
    discuz使用总结
    ThinkPHP3.1快速入门(3)查询语言
    LeetCode 611. 有效三角形的个数(Valid Triangle Number)
    LeetCode 566. 重塑矩阵(Reshape the Matrix)
  • 原文地址:https://www.cnblogs.com/handsomecui/p/5440584.html
Copyright © 2011-2022 走看看