Alice and Bob

Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 3716 Accepted Submission(s): 1179

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

贪心的策略,先进行排序,(两个元素谁优先都一样),对bo[i].y(Bob)进行查找,找到最接近al[i].y(Alice)的值,删除他。

#include <iostream>
#include <set>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm> using namespace std; const int MAX=101000; struct node
{
int w;
int h;
bool operator <(const node &a)const//重载小于号
{
if(w<a.w||(w==a.w&&h<a.h))
{
return true;
}
return false;
}
}al[MAX],bo[MAX]; multiset<int>st; int main()
{
int n;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
st.clear();
for(int i=0;i<n;i++)
{
scanf("%d %d",&al[i].h,&al[i].w);
}
for(int i=0;i<n;i++)
{
scanf("%d %d",&bo[i].h,&bo[i].w);
}
sort(al,al+n);
sort(bo,bo+n);
int ans=0;
int j=0;
multiset<int>::iterator it;
for(int i=0;i<n;i++)
{
while(j<n&&bo[j].w<=al[i].w)//将al[i]可能覆盖的放在set中
{
st.insert(bo[j].h);
j++;
}
if(st.empty())
{
continue;
}
it=st.lower_bound(al[i].h);//二分查找
if(*it==al[i].h)//删除
{
ans++;
st.erase(it);
}
else
{
if(it!=st.begin())//如果返回第一个比它大的
{
st.erase(--it);//删除后面的
ans++;
}
}
}
printf("%d\n",ans);
}
return 0;
}

Alice and Bob(贪心HDU 4268)的更多相关文章

  1. HDU 4268 Alice and Bob 贪心STL O(nlogn)

    B - Alice and Bob Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u D ...

  2. HDU 4268 Alice and Bob(贪心+Multiset的应用)

     题意: Alice和Bob有n个长方形,有长度和宽度,一个矩形能够覆盖还有一个矩形的条件的是,本身长度大于等于还有一个矩形,且宽度大于等于还有一个矩形.矩形不可旋转.问你Alice最多能覆盖Bo ...

  3. hdu 4268 Alice and Bob(multiset|段树)

    Alice and Bob Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) T ...

  4. hdu 4268 Alice and Bob

    Alice and Bob Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tota ...

  5. Alice and Bob HDU - 4268

    Alice and Bob's game never ends. Today, they introduce a new game. In this game, both of them have N ...

  6. hdu 4268 multiset+贪心

    Alice和Bob有n个长方形,有长度和宽度,一个矩形可以覆盖另一个矩形的条件的是,本身长度大于等于另一个矩形,且宽度大于等于另一个矩形,矩形不可旋转,问你Alice最多能覆盖Bob的几个矩形? /* ...

  7. HDU4268 Alice and Bob(贪心+multiset)

    Problem Description Alice and Bob's game never ends. Today, they introduce a new game. In this game, ...

  8. hdu 4111 Alice and Bob 记忆化搜索 博弈论

    Alice and Bob Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...

  9. Alice and Bob HDU - 4111 (SG函数)

    Alice and Bob are very smart guys and they like to play all kinds of games in their spare time. The ...

随机推荐

  1. Java基础之创建窗口——使用SpringLayout管理器(TrySpringLayout)

    控制台程序. 可以把JFrame对象aWindow的内容面板的布局管理器设置为javax.swing.SpringLayout管理器. SpringLayout类定义的布局管理器根据javax.swi ...

  2. 操作系统:进程管理和IO控制

    一.进程管理 进程管理包括进程控制,进程调度,进程同步与通信,死锁控制四个内容. (一)进程控制 进程是操作系统中运行的基本单位,包括程序段,数据段和进程控制段.操作系统通过进程控制块(PCB)管理进 ...

  3. Excel公式无法重算,暂无法解决

    一份复杂的excel报表,某些单元格是用求和公式算出来的值,但生成之后,用excel打开,无法显示公式结果,按F9也没有用,只能在单元格公式双击后回车才会显示.而在WPS2010按F9就可以重算,WP ...

  4. Java I/O解读与使用实例

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主要讲解了Java I/O解读与使用实例. 一.I/O基本概念 I/O全称是Inpu ...

  5. Java堆内存

    Java 中的堆是 JVM 所管理的最大的一块内存空间,主要用于存放各种类的实例对象. 在 Java 中,堆被划分成两个不同的区域:新生代 ( Young ).老年代 ( Old ).新生代 ( Yo ...

  6. exjs3.2的gridPanel的表头总宽度与列的总宽度不一致的解决方案

    修复之前的bug问题 修复办法,谷歌浏览器中,table的单元格实际宽度=指定宽度+padding,所以只要重写gridview里的一个方法: Ext.override(Ext.grid.GridVi ...

  7. JSP-02- 使用JSP实现输出

    二. 使用JSP实现输出 JSP的页面构成: 静态内容.指令.表达式.Scriptlet.声明.动作.注释 Jsp脚本: 表达式.Scriptlet.声明 表达式: <%=  内容  %> ...

  8. 为 Macbook 安装 wget 命令

    没想到装个这个命令那么麻烦, 还要装 xcode?..... 好吧,按步骤来成功装上 http://www.arefly.com/mac-wget/ 其實Mac也自帶了一個Curl同樣也可以下載網頁, ...

  9. laravel5.0升级到laravel5.1

    1.修改composer.json.将其中的"laravel/framework": "5.0.*"修改为"laravel/framework&quo ...

  10. Linux设备模型(9)_device resource management ---devm申请空间【转】

    转自:http://www.wowotech.net/linux_kenrel/device_resource_management.html . 前言 蜗蜗建议,每一个Linux驱动工程师,都能瞄一 ...