思路:(参考网络上的思路)
1)先对坐标排序,若h不等,则对h升序排序,否则对w进行升序排序
2)排序后之后,扫描Alice的手牌,把所有Bob[j].h小于等于Alice[i].h的Bob[j].w放入一个集合中,并且在该集合中找到第一个大于Alice[i].w的Bob[x].w
那么Bob[x-1].w就是跟Alice[i].w最接近的牌,若存在则删除之。
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
#define N 100010
typedef struct Node_
{
int h, w;
}Node;
Node Alice[N];
Node Bob[N];
int cmp(Node a, Node b)
{
if (a.h == b.h)
{
return a.w < b.w;
}
return a.h <= b.h;
}
int main()
{
int t, i, j,n, sum;
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for (i = 0;i < n;++i)
{
scanf("%d %d", &Alice[i].h, &Alice[i].w);
}
for (i = 0;i < n;++i)
{
scanf("%d %d", &Bob[i].h, &Bob[i].w);
}
sort(Alice, Alice + n, cmp);
sort(Bob, Bob + n, cmp);
sum = 0;
multiset<int> m_set;
for (i = 0, j = 0;i < n;++i)
{
while (j < n && Alice[i].h >= Bob[j].h)
{
m_set.insert(Bob[j].w);
++j;
}
if (m_set.size() != 0)
{
multiset<int>::iterator iter = m_set.upper_bound(Alice[i].w);
if (iter != m_set.begin())
{
sum++;
iter--;
m_set.erase(iter);
}
}
}
printf("%d\n", sum);
}
return 0;
}