题意:有Alice和Bob 各有一组矩形,问Alice的矩形最多能够覆盖Bob的矩形前提是A.h>=B.h &&A.w>=B.w;问能够形成多少个矩形覆盖组?
贪心思路:先将两组矩形分别按照h由小到大排序,高度h相等按照 宽度w由小到大;对于A的每一个矩形A[i],找出所有B中所有B.h<=A[i].h组成一个有序集合S,从S中选一个最大的w且 w<=A[i].w。
其中用到两个知识点:lower_bound(),upper_bound();
#include <iostream>
#include <vector>
#include <cstdio>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define pb push_back
#define M 100005
#define CLS(x,v) memset(x,v,sizeof(x))
#define pii pair<int,int>
#define LL long long
struct rect{
int h,w;
bool operator <(const rect &b)const{
if(b.h==h)return w<b.w;
return h<b.h;
}
};
rect a[M],b[M];
multiset<int> s;
int main()
{
int T,n;
scanf("%d",&T);
while(T--)
{
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);
sort(b,b+n);
s.clear();
int j=0;
int ans=0;
multiset<int>::iterator it;
for(int i=0;i<n;i++)
{
//选出a[i]长度能够覆盖的所有矩形,选出一个宽度能够覆盖的宽度最大的
while(j<n&&a[i].h>=b[j].h)
{
s.insert(b[j++].w);
}
if(!s.empty())
{
//在候选矩形中找第一个>a[i].w的
it=s.upper_bound(a[i].w);
if(it!=s.begin())
{
s.erase(--it);
ans++;
}
}
}
printf("%d\n",ans);
}
return 0;
}