hdu (4268)贪心muliset的upper_bound和lower_bound
2012-09-13 15:12
375 查看
有童鞋A 和 童鞋B
A想用手里的牌尽量多地覆盖掉B手中的牌..
给出了T表示有T组样例..
每组样例给出一个n 表示A和B手中都有n张牌
接下来2*n行 有h w 分别代表A手中n张牌的高和宽 以及 B手中n张牌的高和宽
问A手中的牌最多能覆盖B多少张牌
思路:
对一个坐标排序
假设是x坐标
然后扫描
维护一个y坐标
然后每次取的是堆里面最大的
Tips:
set里的一个函数..在短时间内找到 b的w 中满足小于a的w的值~
iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素。
iterator upper_bound( const key_type &key ):返回一个迭代器,指向键值> key的第一个元素。
※ 因为找到的是 >= key的第一个元素..所以为了删除 比当前牌小的..应该it--
Code:
A想用手里的牌尽量多地覆盖掉B手中的牌..
给出了T表示有T组样例..
每组样例给出一个n 表示A和B手中都有n张牌
接下来2*n行 有h w 分别代表A手中n张牌的高和宽 以及 B手中n张牌的高和宽
问A手中的牌最多能覆盖B多少张牌
思路:
对一个坐标排序
假设是x坐标
然后扫描
维护一个y坐标
然后每次取的是堆里面最大的
Tips:
set里的一个函数..在短时间内找到 b的w 中满足小于a的w的值~
iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素。
iterator upper_bound( const key_type &key ):返回一个迭代器,指向键值> key的第一个元素。
※ 因为找到的是 >= key的第一个元素..所以为了删除 比当前牌小的..应该it--
Code:
#include <iostream> #include <cstring> #include <algorithm> #include <string> #include <set> #include <cstdio> #define N 108000 using namespace std; struct Node { int h,w; }alice ,bob ; bool cmp(Node a,Node b) { if(a.h!=b.h)return a.h<=b.h; else return a.w<b.w; } int n; int main() { int cas; scanf("%d",&cas); while(cas--) { scanf("%d",&n); multiset<int> myset; for(int i=0;i<n;i++) scanf("%d%d",&alice[i].h,&alice[i].w); for(int i=0;i<n;i++) scanf("%d%d",&bob[i].h,&bob[i].w); sort(alice,alice+n,cmp); sort(bob,bob+n,cmp); int ans=0; int j=0; for(int i=0;i<n;i++) { while(j<n && bob[j].h<=alice[i].h) { myset.insert(bob[j].w); j++; } if(myset.size()==0)continue; set<int>::iterator it=myset.upper_bound(alice[i].w); if(it!=myset.begin()) { ans++; it--; myset.erase(it); } } printf("%d\n",ans); } return 0; }
相关文章推荐
- hdu 4268 贪心+set lower_bound用法
- hdu 4268 贪心+set lower_bound用法
- HDU 4288 Coder(vector + upper_bound, lower_bound)
- hdu 4288 Coder(线段树+离散化,lower_bound&&upper_bound的运用)
- hdu 4268 Alice and Bob(lower_bound+贪心)
- HDU 4585(Shaolin-Treap的lower_bound&upper_bound操作)
- HDU 4288 Coder(模拟) 附:upper_bound与lower_bound的比较
- HDU 4268 Alice and Bob(lower_bound)
- hdu 4288(lower_bound&&upper_bound的运用)
- HDU 4268 Alice and Bob(lower_bound)
- HDU 5246 超级赛亚ACMer (lower_bound 与 upper_bound)
- hdu 5178(二分-lower_bound,upper_bound)
- HDU 4288 Coder(vector + upper_bound, lower_bound)
- hdu 4302 map练习中 ,lower_bound()和upper_bound()
- upper_bound()与lower_bound()使用方法
- upper_bound,lower_bound,eaual_bound,equal_range二分搜索
- STL中的multimap---顺便说说如何查找同一关键字对应的所有值(利用count, lower_bound/upper_bound, equal_range)
- lower_bound 和 upper_bound
- upper_bound 和lower_bound 的返回值问题
- c++ 对于upper_bound以及lower_bound的说明