• 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;
    }
  • 相关阅读:
    Linux网络相关命令firewalld和netfilter、iptables 使用(6/22)
    Linux时间设置与iptables命令
    负载均衡集群ipvsadm命令及基本用法
    LVS原理详解以及部署
    linux比较两个文件的不同(6/21)
    如何使用sql函数平均值、总数、最小值、最大值
    python中数据类型转换
    使用 getopt 处理命令行长参数
    Mysql常用命令行大全
    C#控制台程序使用Log4net日志组件
  • 原文地址:https://www.cnblogs.com/kuangbin/p/2679227.html
Copyright © 2020-2023  润新知