有n个人,每个人有两个属性x和y。如果对于一个人P(x,y),不存在另外一个人(x1,y1),使得x1<x&&y1<=y 或 x1<=x&&y1<y 的话,那么我们说P是有优势的。每次给出一个人的信息,要求输出在只考虑当前已获得的信息的前提下,多少人是有优势的?
只需要找x左边是否有点的y小于等于y,判断一个点能否插入,插入后还要删除插入后变为无优势的点,利用multiset查找和删除
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
struct point
{
int x,y;
point(int x,int y):x(x),y(y){}
bool operator<(const point&rhs)const
{return x<rhs.x||(x==rhs.x&&y<rhs.y);
}
};
int main()
{
int t;
scanf("%d",&t);
for(int c=1;c<=t;c++)
{
if(c>1)
printf("\n");
printf("Case #%d:\n",c);
multiset<point>s;
multiset<point>::iterator it;
int n;
scanf("%d",&n);
while(n--)
{
int x,y;
scanf("%d%d",&x,&y);
point p(x,y);
it=s.lower_bound(p);
if(it==s.begin()||(--it)->y>p.y)
{s.insert(p);
it=s.upper_bound(p);
while(it!=s.end()&&it->y>=p.y)
s.erase(it++);
}
printf("%d\n",s.size());
}
}
}