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

来源:互联网 发布:数据库中的decode 编辑:程序博客网 时间:2024/06/07 06:46

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 0x7fffffffusing 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;}
0 0
原创粉丝点击