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;
}

转载于:https://www.cnblogs.com/Skyminer/p/7077636.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值