UVA - 11020 - Efficient Solutions (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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值