loj 6085.「美团 CodeM 资格赛」优惠券

题目:

一个有门禁的大楼,初始时里面没有人。
现在有一些人在进出大楼,每个人都有一个唯一的编号。现在有他们进出大楼的记录,但是有些被污染了,只能知道这里有一条记录,具体并不能知道。
一个人只有进大楼,才能出大楼,如果在大楼内,他必须先出去,才能再进来。
现在想知道这个记录是否错误,如果错误,请求出最早的错误在哪一行。
注释:人有无穷多个,记录中没有提到的人也可以进出大楼。

题解:

SB 题

#include <set>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(int &x){
    x=0;static char ch;static bool flag;flag = false;
    while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
    while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
#define rg register int
#define rep(i,a,b) for(rg i=(a);i<=(b);++i)
#define per(i,a,b) for(rg i=(a);i>=(b);--i)
const int maxn = 500010;
set<int>s;
bool exist[maxn];
int last[maxn];
int main(){
    int n;read(n);
    char ch;int x;
    int ans = -1;
    rep(i,1,n){
        while(ch=getchar(),ch<'!');
        if(ch == '?') s.insert(i);
        else{
            read(x);
            if(ch == 'I'){
                if(exist[x]){
                    set<int>::iterator it = s.lower_bound(last[x]);
                    if(it == s.end()){ans = i;break;}
                    else s.erase(it);
                }exist[x] = true;
            }else{
                if(!exist[x]){
                    set<int>::iterator it = s.lower_bound(last[x]);
                    if(it == s.end()){ans = i;break;}
                    else s.erase(it);
                }exist[x] = false;
            }
            last[x] = i;
        }
    }
    printf("%d\n",ans);
    return 0;
}
posted @ 2017-06-25 19:32  Sky_miner  阅读(338)  评论(0编辑  收藏  举报