1. 程式人生 > >UVA 11020 Efficient Solutions(multiset 查詢)

UVA 11020 Efficient Solutions(multiset 查詢)

有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());
       }

    }
}