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. 

InputThe 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. 
OutputFor 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 思路:依旧是贪心,但这个是二维问题,我们可以类比二位偏序问题来处理,先对h排序,扫描一遍Alice的数组,将每个小于等于h的Bob的元素入队,然后将小于等于w的元素出队
记录个数,此时无需关心h,因为入队的元素一定不大于现在的h,这里我们就要用到multiset这个容器来"当作队列",因为其支持upper_bound/lower_bound的二分查找操作,
具体细节用代码感受一下:
struct Node {
int h, w;
Node(int _h = , int _w = ) : h(_h), w(_w){}
bool operator<(const Node &a)const {
return h < a.h || (h == a.h && w < a.w);
}
}; vector<Node> v[];
multiset<int> q; int main() {
int T, n, sum;
scanf("%d", &T);
while(T--) {
q.clear();
sum = ;
scanf("%d", &n);
for (int i = ; i < ; ++i) {
v[i].clear();
for (int j = ; j < n; ++j) {
int t1, t2;
scanf("%d%d", &t1, &t2);
v[i].push_back(Node(t1, t2));
}
}
sort(v[].begin(), v[].end()), sort(v[].begin(), v[].end());
for (int i = , j = ; i < n; ++i) {
while(j < n && v[][i].h >= v[][j].h) {
q.insert(v[][j++].w);
}
if(!q.empty() && *q.begin() <= v[][i].w) {
++sum;
auto tmp = q.upper_bound(v[][i].w);
q.erase(--tmp);
}
}
printf("%d\n", sum);
}
return ;
}

 提一下为什么用upper_bound并递减,因为当此处的w比队列中任意元素大时,两种查找都返回end()迭代器,此时需要递减操作,如果w是中间大小元素,lower_bound的递减就会出错,当然特判一下也可以,等价的。

 

Day3-H-Alice and Bob HDU4268的更多相关文章

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

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

  2. HDU4268 Alice and Bob 【贪心】

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

  3. bzoj4730: Alice和Bob又在玩游戏

    Description Alice和Bob在玩游戏.有n个节点,m条边(0<=m<=n-1),构成若干棵有根树,每棵树的根节点是该连通块内编号最 小的点.Alice和Bob轮流操作,每回合 ...

  4. Alice and Bob(2013年山东省第四届ACM大学生程序设计竞赛)

    Alice and Bob Time Limit: 1000ms   Memory limit: 65536K 题目描述 Alice and Bob like playing games very m ...

  5. sdutoj 2608 Alice and Bob

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2608 Alice and Bob Time L ...

  6. hdu 4268 Alice and Bob

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

  7. SDUT 2608:Alice and Bob

    Alice and Bob Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描述 Alice and Bob like playing ...

  8. Alice and Bob(贪心HDU 4268)

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

  9. UVA 1484 - Alice and Bob&#39;s Trip(树形DP)

    题目链接:1484 - Alice and Bob's Trip 题意:BOB和ALICE这对狗男女在一颗树上走,BOB先走,BOB要尽量使得总路径权和大,ALICE要小,可是有个条件,就是路径权值总 ...

  10. ZOJ 3757 Alice and Bob and Cue Sports(模拟)

    题目链接 题意 : 玩台球.Alice 和 Bob,一共可以进行m次,Alice 先打.有一个白球和n个标有不同标号的球,称目标球为当前在桌子上的除了白球以外的数值最小的球,默认白球的标号为0.如果白 ...

随机推荐

  1. python3下pygame显示中文的设置

    1.先看代码: import pygame from pygame.locals import * def main(): pygame.init() screen = pygame.display. ...

  2. 配置SVTI

    路由器SVTI站点到站点VPN         在IOS 12.4之前建立安全的站点间隧道只能采用GRE over IPSec,从IOS 12.4之后设计了一种全新的隧道技术,即VIT(Virtual ...

  3. Postgresql查询表和表结构

    查询表名 SELECT tablename FROM pg_tables WHERE tablename NOT LIKE 'pg%' AND tablename NOT LIKE 'sql_%' O ...

  4. Android读取权限

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <use ...

  5. linux文件或目录属性

    wc(word count)命令的功能:统计指定文件的字节数.字数.行数.,并将统计结果显示输出 命令参数: -c 只显示字节数 -l    只显示行数 -w 只显示字数 od命令:查看二进制文件信息 ...

  6. Spring中@MapperScan注解

    之前是,直接在Mapper类上面添加注解@Mapper,这种方式要求每一个mapper类都需要添加此注解,麻烦. 通过使用@MapperScan可以指定要扫描的Mapper类的包的路径,比如: @Sp ...

  7. Flask - 运行APP

    from flask import Flask app = Flask(__name__) @app.route("/") def hello(): return 'Hello, ...

  8. hutoolJava工具类的使用

    前言 安装 友情开源项目 Hutool相关博客(软文) 捐赠使用公开 核心(Hutool-core) 克隆 支持泛型的克隆接口和克隆类 类型转换 类型转换工具类-Convert 自定义类型转换-Con ...

  9. char、pchar、string互相转换

    1.string转换成pchar 可以使用pchar进行强制类型转换,也可以使用StrPCopy函数 var s:string; p,p1:PChar; begin s:='Hello Delphi' ...

  10. 十三 Spring的通知类型,切入表达式写法

    Spring中通知类型: 前置通知:目标方法执行之前进行操作,可以获得切入点信息 后置通知: 目标方法执行之后进行操作,可以获得方法的返回值 环绕通知:在目标方法执行之前和之后进行操作,可以终止目标方 ...