题意: 要求找出 Alice 的卡片覆盖 Box的卡片的最大张数, 长和宽都大于等于另一张卡片则能够覆盖..
思路: 贪心思想+STL
一开始想到的是赛马问题, 从最大的开始比, WA 了, 问了下静姐才明白, 不能从最大比, 而应该从最小比, 因为 最小才是最难匹配到的...
比如这个例子: A: (2,2) ,(2,4) ,(3,5)
B: (1,5) , (2,4), (4,6) 若从大到小比较则答案为1, 实际上最大的答案为 2...
接着 又WA 了N 遍 , 没有 容器清空!!!!
# lower_bound 的使用:
se[]={1,3,4,5,6};
set<int> se;
set<int>::iterator it=se.lower_bound(num) ; // 返回到第一个大于等于 num 的数的指针~ lower_bound(2)=3, lower_bound(3)=3,目标对象存在即为目标对象的位置,不存在则为后一个位置.
# upper_bound的使用:
// 返回>对象的第一个位置, upper_bound(2)=3,upper_bound(3)=4, 无论是否存在都为后一个位置.
#binary_search:判断是否存在某个对象
#equal_bound: 返回由lower_bound和upper_bound返回值构成的pair,也就是所有等价元素区间。
equal_bound有两个需要注意的地方:
1. 如果返回的两个迭代器相同,说明查找区间为空,没有这样的值
2. 返回迭代器间的距离与迭代器中对象数目是相等的,对于排序区间,他完成了count和find的双重任务
以上的函数处理的都是有序序列~
以下的函数不需要有序序列~~~~
#count:计算对象区间中的数目。
#find:返回第一个对象的位置。
查找成功的话,find会立即返回,count不会立即返回(直到查找完整个区间),此时find效率较高。
因此除非是要计算对象的数目,否则不考虑count。
CODE:
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<set>
using namespace std;
struct node
{
int h,w;
}a[100005],b[100005];
bool cmp(node a,node b)
{
if(a.h==b.h) return a.w<b.w;
return a.h<b.h;
}
multiset<int> mul;
int main()
{
freopen("in.in","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
mul.clear();
int n,hh,ww;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d%d",&a[i].h,&a[i].w);
}
for(int i=0;i<n;i++)
{
scanf("%d%d",&b[i].h,&b[i].w);
}
sort(a,a+n,cmp); //printf("%d %d\n",a[1].h,a[1].w);
sort(b,b+n,cmp);
int ans=0;
int k=0;
for(int i=0;i<n;i++)
{
while(k<n && b[k].h<=a[i].h)
{
mul.insert(b[k].w);
k++;
}
if(mul.size() >= 1)
{
multiset<int>::iterator it=mul.lower_bound(a[i].w);
{
if(it==mul.end()) it--;
if(it!=mul.begin() && *it>a[i].w) it--;
if(*it <= a[i].w)
{
mul.erase(it);
ans++;
}
}
}
}
printf("%d\n",ans);
}
return 0;
}