UVA 11020 multiset、lower_bound、 upper_bound STL里面的排序二叉树

http://uva.onlinejudge.org/index.phpion=com_onlinejudge&Itemid=8~~~~~

Problem I
Efficient Solutions

Input: Standard Input

Output: Standard Output

"Our marriage ceremonies are solemn, sober
moments of reflection; also regret, disagreement,
argument and mutual recrimination. Once you know
it can't get any worse, you can relax and enjoy
the marriage."

J. Michael Straczynski, "The Deconstruction of Falling Stars."

The princess of Centauri Prime is the galaxy's most eligible bachelorette of the year. She has hopeful grooms lined up in front of the royal palace for a chance to spend 5 minutes to try and impress her. After 5 minutes, the gentleman is carried out of the royal chambers by the palace guards, and the princess makes a decision. She rates the lad on his lineage and charm by giving him a score for each of the two properties. On Centauri Prime, low scores are better than high scores.

Suppose that she observes two gentlemen - A and B. She assigns A the scores LA and CA (for lineage and charm, respectively). B receives scores LB and CB. Then A is dominated by B if either

  • LB < LA and CB <= CA, or
  • LB <= LA and CB < CA.

In other words, if at least one of B's scores is better than A's, and the other score is not worse. She considers a gentleman to be efficient (or Pareto-optimal) if she has not yet met any other gentleman who dominates him. She maintains a list of efficient grooms and updates it after each 5-minute presentation.

Given the queue of bachelors and the scores assigned to them by the princess, determine the number of entries in the list of efficient groomsafter each performance.

Input
The first line of input gives the number of cases, N (0<N<40)N test cases follow.

Each one starts with a line containing n (0≤n≤15000) - the size of the queue. The next n lines will each contain two scores (integers in the range [0, 109]). Initially, the list is empty.

Output
For each test case, output one line containing "Case #x:" followed by n lines, line i containing the size of the list of efficient grooms after the ithupdate. Print an empty line between test cases.

 

Sample Input

Sample Output

4
1
100 200
2
100 200
101 202
2
100 200
200 100
5
11 20
20 10
20 10
100 20
1 1
Case #1:
1
 
Case #2:
1
1
 
Case #3:
1
2
 
Case #4:
1
2
3
3
1

题目大意:有n个人,每个人有两个属性x和y。如果对于一个人p(x,y),不存在另外一个人(x’,y‘),使得x'<x,y'<=y,或者x'<=x,y'<y,我们说p是有优势的。每次给出一个人的信息,要求输出在只考虑当前已获得的信息的前提下,都少人是有优势的。

解题思路:

              注意到人只是只增不减的,因此一个现在又有优势的人,以后可能会失去优势,而且一旦失去优势,便再也不会重新获得优势。这样可以动态维护优势人群集合。如果用平面坐标上的点表示一个人,那么优势人群对应的集合会是什么样的呢?

             首先根据定义,不会有两个不同的点x坐标或者y坐标相同。其次,对于任意两个点A和B,如果A在B的左边(即A的x 坐标严格小于B的x坐标),那么A一定在B的上边(即A的坐标严格大于B的y坐标)。这样,左到右看,各个点的y坐标越来越小。新增一个点P后:

           情况一:这个点可能本身没有优势,因此直接忽略这个点即可,判断方法很简单,只需判断它左边相邻的点的y坐标是否比它小即可。

           情况二:这个点可以有优势,则把它加入到集合中。注意这个点可能会让其他点失去优势,从而被删除。

这里采用STL里的multiset(可重集)来表示这个点集(因为集合中可以有相同点,代表属性完全相同的人),则第一种情况只需要比较lower_bound(p)和p的y坐标即可。而第二种情况只需要从upper_bount(P)开始,删除所有没有优势的点。

           另外在用自定义的结构体数据充当multiset或set的成员时必须要注意重载运算符"<"(一般不用大于号)定义一个排序的标准。因为set只不过是STL实现的特殊排序二叉树里面的元素都是有序的。

#include <stdio.h>
#include <set>
using namespace std;

struct Point
{
    int a,b;
    Point(int x,int y)
    {
        a=x;
        b=y;
    }
    bool operator < (const Point& rhs)const
    {
        return a<rhs.a||(a==rhs.a&&b<rhs.b);
    }
};
multiset<Point>S;
multiset<Point>::iterator it;
int main()
{
    int T;
    scanf("%d",&T);
    for(int kase=1; kase<=T; kase++)
    {
        if(kase>1)
            printf("\n");
        printf("Case #%d:\n",kase);
        int n,a,b;
        scanf("%d",&n);
        S.clear();
        while(n--)
        {
            scanf("%d%d",&a,&b);
            Point p=Point(a,b);
            it=S.lower_bound(p);
            if(it==S.begin()||(--it)->b>b)
            {
                S.insert(p);
                it=S.upper_bound(p);
                while(it!=S.end()&&it->b>=b)
                    S.erase(it++);
            }
            printf("%d\n",S.size());
        }
    }
    return 0;
}
/**
4
1
100 200
2
100 200
101 202
2
100 200
200 100
5
11 20
20 10
20 10
100 20
1 1
**/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值