HDU 4268 Alice and Bob(贪心)
来源:互联网 发布:网络体系架构 编辑:程序博客网 时间:2024/05/29 19:15
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4268
一般像这种题目,首先会想到动态规划算法但是这个题目一看数据就想到肯定是要找一个O(n)的或者O(n*logn)的一个算法,所以动态规划这种
算法比较难找,也很少见这种低复杂度的动态规划算法,所以就只能是贪心能提供这种复杂度了。
把两个人的牌揉到一起,做个标记是alice的还是bob的,然后按照长排序,长相等按照宽排序,都相等就alice在后bob在前面
然后从前向后遍历,如果是bob的,直接将bob的宽插入的set中,如果是alice,则让alice的这张牌去覆盖set里面比alice的宽
小或相等的最大的宽,然后从set中删除,这里为什么宽符合条件就能覆盖呢,因为前面按照长从小到大排序了,所以说这个
是一个很明显的贪心!
#include <iostream>#include <string.h>#include <stdio.h>#include <algorithm>#include <set>using namespace std;#define maxn 110000struct point{ int length; int width; int first;}po[maxn*2];int n;multiset<int> my_set;bool cmp(const point &a,const point &b){ if(a.length == b.length) { if(a.width == b.width) return a.first < b.first; return a.width < b.width; } return a.length < b.length;}multiset<int>::iterator it;int find_ans(){ int ans=0,i,j,k; for(i=0;i<n;i++) { if(po[i].first==0) my_set.insert(po[i].width); else { if(!my_set.empty() && *my_set.begin() <= po[i].width) { it=my_set.upper_bound(po[i].width); it--; my_set.erase(it); ans++; } } } return ans;}int main(){ int t,i,j,k; scanf("%d",&t); while(t--) { my_set.clear(); scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d%d",&po[i].length,&po[i].width); po[i].first=1; } n*=2; for(i;i<n;i++) { scanf("%d%d",&po[i].length,&po[i].width); po[i].first=0; } sort(po,po+n,cmp); printf("%d\n",find_ans()); } return 0;}
- 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 (set+贪心)
- HDU 4268 Alice and Bob(贪心+数据结构)
- hdu 4268 Alice and Bob(贪心)
- HDU 4268 Alice and Bob (set+贪心)
- HDU 4268 Alice and Bob(贪心+multiset)
- Alice and Bob(贪心HDU 4268)
- HDU 4268-Alice and Bob-贪心
- hdu 4268 Alice and Bob矩形覆盖(贪心)
- HDU 4268 Alice and Bob(贪心+Multiset的应用)
- HDU 4268 Alice and Bob(贪心+STL)
- hdu 4268 Alice and Bob(lower_bound+贪心)
- 贪心,STL,multiset(Alice and Bob,HDU 4268)
- HDU 4268 Alice and Bob(贪心+STL)
- Unity3D之NGUI 系列教程(一)
- iphone静态库的应用
- 论文回顾之一 一种新的直线段检测算法---LSD:a Line Segment Detector
- GSL向量文件存储
- POJ1113 Wall
- HDU 4268 Alice and Bob(贪心)
- 为TextView设置滚动的方法
- uva -- 10194 - Football (aka Soccer)
- 学《笨办法学python》(2)
- 数据在内存中的二进制表示和IEEE754
- 乐观锁和悲观锁 你更钟情于哪一个?
- c++连接mongodb出错
- 最新Eclipse配置Android开发环境SDK 并安装JellyBean 4.3的方法
- 关于main函数中的两个参数