码迷,mamicode.com
首页 > 其他好文 > 详细

UVA - 11020 - Efficient Solutions (multiset实现BST)

时间:2015-08-01 19:09:41      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:uva   acm   multiset   bst   

Efficient Solutions

题目传送:Efficient Solutions

AC代码:

#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;

int n;

struct node {
    int x, y;
    node(int _x, int _y) : x(_x), y(_y) {
    }
    node() {
    }
    bool operator < (const node& a) const {
        return x < a.x || (x == a.x && y < a.y);
    }
};

multiset<node> s;
multiset<node>::iterator it;

int main() {
    int T;
    scanf("%d", &T);
    for(int cas = 1; cas <= T; cas ++) {
        if(cas > 1) printf("\n");
        printf("Case #%d:\n", cas);
        scanf("%d", &n);

        s.clear();
        for(int i = 0; i < n; i ++) {
            int x, y;
            scanf("%d %d", &x, &y);
            node t = node(x, y);
            it = s.lower_bound(t);//返回第一个大于等于t的元素的位置 
            if(it == s.begin() || (--it)->y > y) {
                s.insert(t);
                it = s.upper_bound(t);//返回第一个比t大的元素的位置 
//              while(it != s.end() && it->y >= y) s.erase(it ++);  
                for(; it != s.end() && it->y >= y; ) { 
                    s.erase(it ++);//只能通过这种方法删除set里的连续的一段,如果it++写外面就会出错,因为it在之前所指向的值已经被删除了 
                    //迭代器it是通过++运算指向后一个元素的,所以只能通过在删除的时候一起++才行 
                }
            }
            printf("%d\n", s.size());
        }

    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

UVA - 11020 - Efficient Solutions (multiset实现BST)

标签:uva   acm   multiset   bst   

原文地址:http://blog.csdn.net/u014355480/article/details/47188135

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!