Uva11020 Efficient Solutions 熟练使用语言自带BST(map/set)

题意  

  N个人,每个人有一个属性(x,y), 若对于一个人P(x,y),不存在P`(x`,y`) 

使 x`< x , y` <= y or x` <= x , y` < y , 则认为 P是有优势的.问逐一的输入N个人

信息,在当前情况下有优势的人的数量.

解题思路

  若添加当前人信息进入, P(x,y), 其可能有两种情况:

    1. 若 存在 P`(x`,y`), 使得 x`<x,y`<=y or x`<=x,y`<y, 则P无优势,则不必加入.

    2. 若其不存在则其 有优势,并会导致部分点失去优势,将那些点删除.

  这里 通过 <x,y> 进行排序. 使用 STL 的 mutilset 因为允许重复.

View Code
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<set>
#include<algorithm>
using namespace std;

multiset< pair<int,int> > S;
multiset< pair<int,int> >::iterator it;

int main(){
    int T;
    scanf("%d", &T);
    for(int Case = 1; Case <= T; Case++ ){
        int a, b, n;
        scanf("%d", &n);
        printf("Case #%d:\n", Case );
        S.clear();
        for(int i = 0; i < n; i++){
            scanf("%d%d", &a,&b);
            pair<int,int> P = make_pair(a,b);
            it = S.lower_bound( P );
            if( it == S.begin() || ((--it)->second > b) ){
                S.insert( P );
                it = S.upper_bound( P );
                while( it != S.end() && it->second >= b ) S.erase( it++ );        
            }
            printf("%d\n", S.size() );
        }
        if( Case > 1 ) puts("");    
    }
    return 0;    
}
原文地址:https://www.cnblogs.com/yefeng1627/p/3008668.html