cf 567B - Berland National Library

  这题就是开一个标记数组,标记哪些人走了哪些人进来了,在用now维护当前进来了的总人数,maxx维护当前已知的最大总人数,主要是当有人离开时要分类讨论,如果此人没被标记,说明在系统启动之前便进来了,所以maxx++,如果被标记了,说明是在系统启动之后进入的。

#include<bits/stdc++.h>
using namespace std;
int vis[1000000+10]={0};
int n;
int main()
{
    cin>>n;
    char ch;
    int num,now=0,maxx=0;
    for(int i=1;i<=n;i++)
    {
        scanf(" %c%d",&ch,&num);
        if(ch=='+')
        {
            now++;
            vis[num]=1;
            maxx=max(maxx,now);
        }
        else
        {
            if(vis[num])
            {
                now--;
                vis[num]=0;
            }
            else
            {
                maxx++;
            }
        }
    }
    cout<<maxx<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40642465/article/details/81358049