http://acm.hdu.edu.cn/showproblem.php?pid=4268
12长春网络。
题意:alice和bob各有n张牌,当某张牌长宽均不小于另一张牌的长宽时,则可覆盖在另一张上,求alice最多可以覆盖bob多少张牌,一张只能使用一次。
贪心。先按x排序,对于当前alice的牌A,找出bob牌中x比A.x小的那些牌中y不超过A.y中的y最大的那张。由于已按x排序,所以这样贪心是正确的。找不超过A.y的最大的y采用multiset。
//返回元素值为elem的元素个数
s.count(elem)
//返回第一个元素值为 elem 的位置,找不到的话返回end()
s.find(elem)
//返回 elem 的第一个可以安插的位置,也就是 (元素值 >=elem第一个)的位置
s.lower_bound(elem)
//返回 elem 的最后一个可以安插的位置,也就是(元素值 > elem第一个)的位置
s.upper_bound(elem)
//返回 elem 可以安插的第一个位置和最后一个位置,也就是(元素值 == elem) 的元素区间
s.equal_range(elem)
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <iostream>
#include<cstdlib>
#include <algorithm>
#include <string>
//#include <time.h>
using namespace std;
const double eps=1e-7;
const int INF=(1<<30)-1;
//const double INF=1e50;
//const double pi=acos(-1);
#define N 100005
#define M 100005
int n;
struct rec
{
int x,y;
}a[N],b[N];
multiset<int> l;
bool cmp(const struct rec &p,const struct rec &q)
{
if (p.x!=q.x) return p.x<q.x;
else return p.y<q.y;
}
int main()
{
freopen("a","r",stdin);
int T,kk,i,j,sum,k;
multiset<int>::iterator it;
scanf("%d",&T);
for (kk=1;kk<=T;kk++)
{
scanf("%d",&n);
for (i=1;i<=n;i++)
scanf("%d%d",&a[i].x,&a[i].y);
for (i=1;i<=n;i++)
scanf("%d%d",&b[i].x,&b[i].y);
sum=0;
sort(a+1,a+1+n,cmp);
sort(b+1,b+1+n,cmp);
l.clear();
j=0;
for (i=1;i<=n;i++)
{
for (k=j+1;b[k].x<=a[i].x && k<=n;k++) l.insert(b[k].y);
j=k-1;
it=l.upper_bound(a[i].y);
if (it!=l.begin())
{
it--;
l.erase(it);
sum++;
}
}
printf("%d\n",sum);
}
return 0;
}