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的递减就会出错,当然特判一下也可以,等价的。

 

最新文章

  1. php Your system does not support any of these drivers: gmagick,imagick,gd2
  2. 使用Axis2 插件 报错&quot;An error occurred while completing process -java.lang.reflect.InvocationTargetException&quot;
  3. Hadoop组件之-HDFS(HA实现细节)
  4. 在Label中显示一段文字
  5. queue-fun —— nodejs下基于Promise的队列控制模块。
  6. CAF(C++ actor framework)使用随笔(projection 用法)(一)
  7. POJ 2891 扩展欧几里德
  8. [Struts2学习笔记] -- 环境配置
  9. python 重要模块
  10. GPU编程-Thread Hierarchy(3)
  11. 让PIP源使用国内镜像,提升下载速度和安装成功率。
  12. 【mysql】 操作 收集持续更新
  13. pydensecrf的inference.py代码的学习
  14. php项目中使用element.ui和vue
  15. Source Qualifter组件中sqlquery过长导致截取
  16. composer更改源为国际
  17. 13.翻译系列:Code-First方式配置多对多关系【EF 6 Code-First系列】
  18. Centos7.2下OpenVPN 环境完整部署记录
  19. 一、Html5基础讲解以及五个标签
  20. tomcat 的自问自答与总结

热门文章

  1. 【快学SpringBoot】快速上手好用方便的Spring Cache缓存框架
  2. Vue——解决移动端键盘弹起导致的页面fixed定位元素布局错乱
  3. linux与python3安装redis
  4. 加密设备NAT对IPSec的影响
  5. 2019年7月22日A股科创板开板首日行情思考
  6. 【洛谷P3500】TES-Intelligence Test
  7. 全排列next_permutation()用法和构造函数赋值
  8. base64,base32bit加密解密
  9. 「AHOI2014/JSOI2014」骑士游戏
  10. Linux 7 和 CentOS 7 收到重要内核安全更新