UVA 11020 Efficient Solutions (用multiset實現BST)

題目鏈接:https://cn.vjudge.net/problem/UVA-11020

“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 grooms after 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 i-th update. Print an empty line between test cases.
Sample Input
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
Sample Output
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是有優勢的。每次給出一個人的信息,要求輸出在只考慮當前已獲得的信息的前期下,多少人是有優勢的。
輸入格式
整數T代表T組數據,每組數據一個(0<=n<=15000),一下n行,每行兩個整數x,y,分別代表每個人的兩個屬性。
輸出格式
對於每組數據,輸出獲得每條信息後,有優勢的人數。
【分析】
我們可以看到人是隻增不減的,所以當下有優勢的人在以後可能會失去優勢,而且一旦失去優勢的話以後不會再獲得優勢,因此我們可以動態維護優勢人羣集合。
新增加一個人後會有兩種情況:
1.這個點本身沒有優勢,可以直接忽略。
2.這個點有優勢,我們把它加入到集合中,但這個點可能會使其他點失去優勢,從而被刪除。
這裏採用STL中的multiset(可重集)來實現,因爲集合中可以有完全相同的點。
【AC代碼 】

#include<stdio.h>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<set>
using namespace std;
#define MAX_N 10000005
#define LL long long

struct Point
{
    int a,b;
    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,iCase=0;
    scanf("%d",&T);
    while(T--)
    {
        int n,a,b;
        scanf("%d",&n);
        printf("Case #%d:\n",++iCase);
        S.clear();
        for(int i=1;i<=n;i++)
        {
            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());
        }
        if(T>0)printf("\n");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章