Alice and Bob(贪心HDU 4268)
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)的更多相关文章
- HDU 4268 Alice and Bob 贪心STL O(nlogn)
B - Alice and Bob Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u D ...
- HDU 4268 Alice and Bob(贪心+Multiset的应用)
题意: Alice和Bob有n个长方形,有长度和宽度,一个矩形能够覆盖还有一个矩形的条件的是,本身长度大于等于还有一个矩形,且宽度大于等于还有一个矩形.矩形不可旋转.问你Alice最多能覆盖Bo ...
- hdu 4268 Alice and Bob(multiset|段树)
Alice and Bob Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
- hdu 4268 Alice and Bob
Alice and Bob Time Limit : 10000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Tota ...
- 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 ...
- hdu 4268 multiset+贪心
Alice和Bob有n个长方形,有长度和宽度,一个矩形可以覆盖另一个矩形的条件的是,本身长度大于等于另一个矩形,且宽度大于等于另一个矩形,矩形不可旋转,问你Alice最多能覆盖Bob的几个矩形? /* ...
- HDU4268 Alice and Bob(贪心+multiset)
Problem Description Alice and Bob's game never ends. Today, they introduce a new game. In this game, ...
- hdu 4111 Alice and Bob 记忆化搜索 博弈论
Alice and Bob Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pi ...
- 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 ...
随机推荐
- Struts2 用 s:if test 判断属性和字符串相等时 注意双引号和单引号的使用
字符串N一定要用“”双引号包含,从test的包含则用单引号 ‘ ’,如果相反,则不能正确判断该属性是否与该字符串相等. 正确:<s:if test='activityBean.searchFor ...
- Java基础之在窗口中绘图——使用模型/视图体系结构在视图中绘图(Sketcher 1 drawing a 3D rectangle)
控制台程序. 在模型中表示数据视图的类用来显示草图并处理用户的交互操作,所以这种类把显示方法和草图控制器合并在一起.不专用于某个视图的通用GUI创建和操作在SketcherFrame类中处理. 模型对 ...
- [reprint]malloc与calloc的区别
[http://blog.163.com/crazy20070501@126/] 转自某自由人的博客: malloc与calloc的区别 函数malloc()和calloc()都可以用来动态分配内存空 ...
- 使用sql对数据库进行简单的增删改查
1.创建表 create table 表名( 列名 列的类型, 列名 列的类型, 列名 列的类型 (注意自后一列不能加‘ ,’) ); 2.修改表 修改表名--> rename 旧表名 t ...
- Android 沉浸式状态栏
1,传统的手机状态栏是呈现出黑色或者白色条状的,有的和手机主界面有很明显的区别.这样就在一定程度上牺牲了视觉宽度,界面面积变小.看一下QQ的应用 2,实现起来也挺简单的,来一起看一下吧 MainAct ...
- 《zw版·Halcon-delphi系列原创教程》cgal与opencv,Halcon
<zw版·Halcon-delphi系列原创教程>cgal与opencv,Halcon opencv作为少有的专业开源图像软件,虽然功能,特别是几何计算方面,不如Halcon,不过因为开源 ...
- SQLServer查询速度慢的原因
查询速度慢的原因很多,常见如下几种: 1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷) 2.I/O吞吐量小,形成了瓶颈效应. 3.没有创建计算列导致查询不优化. 4.内存 ...
- java中的拷贝(二)深克隆
浅拷贝(Object类中的clone()方法)是指在拷贝对象时,对于基本数据类型的变量会重新复制一份,而对于引用类型的变量只是对引用进行拷贝. 深拷贝(或叫深克隆) 则是对对象及该对象关联的对象内容, ...
- COM编程之三 QueryInterface
[1]IUnknown接口 客户同组件交互都是通过接口完成的. 在客户查询组件的其它接口时,也是通过接口完成的.而那个接口就是IUnknown. IUnknown接口的定义包含在Win32SDK中的U ...
- 160905、c3p0详细配置
官方文档 : http://www.mchange.com/projects/c3p0/index.html <c3p0-config> <default-config> &l ...