UVA 11020 multiset、lower_bound、 upper_bound STL裡面的排序二叉樹

life4711發表於2014-08-11

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
**/


相關文章