hdu 4268 Alice and Bob (set+贪心)
来源:互联网 发布:淘宝 优惠券 设置 编辑:程序博客网 时间:2024/06/05 09:12
Alice and Bob
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1796 Accepted Submission(s): 641
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.
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.
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
感想:比赛时一看到这题,感觉是个很水的贪心,不过后来又觉得不是那么简单,关键是自己set、平衡树不会啊,太菜了,会一点都能把这个贪心做出来了。
思路:将两个人的矩形一起排序(按h、w、id顺序),然后扫描一遍,找满足条件的最近矩形对。(如果一个矩形有多个能覆盖的,则覆盖w最大的)
ps:亲们 不好意思 开始用set过了 以为用set就够了 不过set里面的元素不允许重复 所以有些情况会出错 ╮(╯▽╰)╭ 杭电的数据又一次水了。。。 思路都是一样的 把set改为multiset就够了 感谢泉泉的提醒
代码:
#include <iostream>#include <cstdio>#include <set>#include <algorithm>#define maxn 200005using namespace std;int n,m,ans;struct Node{ int h,w,id;}node[maxn];multiset<int>s;multiset<int>::iterator ite;bool cmp(const Node&x1,const Node&x2){ if(x1.h!=x2.h) return x1.h<x2.h; if(x1.w!=x2.w) return x1.w<x2.w; return x1.id<x2.id;}int main(){ int i,j,t; scanf("%d",&t); while(t--) { scanf("%d",&n); for(i=1;i<=2*n;i++) { scanf("%d%d",&node[i].h,&node[i].w); node[i].id=(i<=n); } sort(node+1,node+2*n+1,cmp); // 按 h、w、id 的顺序排序 s.clear(); ans=0; for(i=1;i<=2*n;i++) { if(!node[i].id) s.insert(node[i].w); else { if(s.empty()||node[i].w<*(s.begin())) continue ; // s为空的时候也直接跳过 ite=s.upper_bound(node[i].w); // 找到w大于等于他的 则他的上一个w肯定小于他 ite--; ans++; s.erase(ite); // 覆盖后删除 } } printf("%d\n",ans); } return 0;}/*132 310 86 81 24 85 8*/
- hdu 4268 Alice and Bob (set+贪心)
- HDU 4268 Alice and Bob (set+贪心)
- HDU 4268 Alice and Bob 贪心+set应用
- 【set+贪心】HDU 4268——Alice and Bob
- hdoj 4268 Alice and Bob 【贪心 + set】
- HDU 4268 Alice and Bob(贪心+数据结构)
- HDU 4268 Alice and Bob(贪心)
- hdu 4268 Alice and Bob(贪心)
- HDU 4268 Alice and Bob(贪心)
- HDU 4268 Alice and Bob (贪心)
- HDU 4268 Alice and Bob(贪心+multiset)
- HDU 4268 Alice and Bob(贪心)
- Alice and Bob(贪心HDU 4268)
- HDU 4268-Alice and Bob-贪心
- hdu 4268 Alice and Bob(贪心)
- HDU 4268 Alice and Bob set用法
- HDU 4268-Alice and Bob( STL: set)
- HDU4268 Alice and Bob【贪心+SET】
- android关于AndroidManifest.xml详细分析
- 查询oracle表字段名称
- cocos2d-x按钮CCControlButton的用法
- Spring3核心技术之JdbcTemplate
- 服务器被攻击处理异常流量的方法
- hdu 4268 Alice and Bob (set+贪心)
- linux thread 命名
- HDU 4277 USACO ORZ 搜索
- 三极管放大电路的输出电阻
- 找单链表环的入口点
- 关于本地程序启动其他程序的方法
- 关于任意两点之间移动的一些算法
- Java菜鸟学习笔记--面向对象篇(十四):继承与组合
- 黑马程序员--java代理