iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素。
iterator upper_bound( const key_type &key ):返回一个迭代器,指向键值> key的第一个元素。
对Alice和Bob的数据一起排序,再贪Alice离Bob最进的矩形对 (前面有bug 感谢
u010228612 的帮助 将set 改为 multiset 非唯一的···)
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<set>
using namespace std;
int n;
struct node
{
int h,w,id;
}no[200000];
bool cmp(node a,node b)
{
if(a.h != b.h) return a.h < b.h;
if(a.w != b.w) return a.w < b.w;
return a.id < b.id;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i =0;i<2*n;i++)
{
scanf("%d%d",&no[i].h,&no[i].w);
no[i].id = i<n;
}
sort(no,no+2*n,cmp);
multiset <int > se;
se.clear();
int ans=0;
for(int i =0;i<2*n;i++)
{
if(no[i].id==0) se.insert(no[i].w);
else
{
if(!se.empty())
{
if(no[i].w < *se.begin() ) continue;
multiset <int >::iterator p = se.upper_bound(no[i].w);
p--;
ans++;
se.erase(p);
}
}
}
printf("%d\n",ans);
}
}