hdu4268 Alice and Bob

来源:互联网 发布:江苏移动网络怎么样 编辑:程序博客网 时间:2024/05/17 07:05

Alice and Bob

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


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
221 23 42 34 532 35 76 84 12 53 4
 

Sample Output
12
 

Source
2012 ACM/ICPC Asia Regional Changchun Online
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:  4276 4274 4272 4277 4267 

普通的双重循环会超时,另外使用c++的输出方式也会超时,这一点真的好坑啊。

利用multiset来解答问题。 这个容器和set的不同点就是允许有重复值。先对两个数组排序,之后遍历第一个数组,设当前为第i个,我们需要满足两个条件,我们分开判断,先判断满足第一个条件,也就是长度的。while循环把第二个数组所有长小于等于i的长度的数组的宽全部放进容器当中(注意不要重复放入,也就是说while循环进入的就是进入了,下一次遍历第一个数组时要接着来,不要从0开始遍历第二个数组) 。之后利用lower_bound查找第一个大于等于第i个的宽的值。如果没有查到,continue;如果正好相等,数量+1,利用erase把那个宽删除,还有一个情况,直接看代码


题目总结:对于两个判断条件,可以选择利用容器先判断一个,在判断另一个。节约时间。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<set>
using namespace std;
int t;
int n;
int k;
struct node
{


    int x,y;


};
bool cmp(node x1,node x2)
{
        if(x1.x==x2.x)
          return x1.y<x2.y;
        return x1.x<x2.x;


}
node t1[1000010];
node t2[1000010];
multiset <int> s;
multiset<int>::iterator it;
int cnt;
int main()
{
    cin>>t;
    while(t--)
    {


        cin>>n;
        s.clear();
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d",&t1[i].x,&t1[i].y);


        }
         for(int i=1;i<=n;i++)
        {
          scanf("%d%d",&t2[i].x,&t2[i].y);
        }
        sort(t1+1,t1+n+1,cmp);
        sort(t2+1,t2+n+1,cmp);
        cnt=0;
        k=1;
        for(int i=1;i<=n;i++)
        {
           while(k<=n&&t2[k].x<=t1[i].x)
           {
               s.insert(t2[k].y);
               k++;
           }
           if(s.empty())
           {
               continue;


           }
           it=s.lower_bound(t1[i].y);
           if(*it==t1[i].y)
           {
               cnt++;
               s.erase(it);
           }
           else
           {


              if(it==s.begin())
               {
                  continue;
                }
               else
              {


                  s.erase(--it); //这里为什么要提前减一。  我们查询的是大于等于i的宽的第一个值。如果等于,最好,前面也判断了。 如果不等于,我们肯定要往前找,只要往前1个,那肯定就是小于i的宽了。 这是最优值,也就是保证可以覆盖情况下,最大的选择
                  cnt++;
                }


             }
        }
        printf("%d\n",cnt);


    }
}