http://codeforces.com/problemset/problem/567/B

 

题意:图书馆有一个程序,可以记录学生的进出情况,'+'表示进入,'-'表示出去,字符后面的数字表示学生的编号。在这个程序尚未启动前,还有一些同学进的消息没有被记录下来。现在问你这个图书馆至少得容纳多少学生。

 

分析:用sum和ans两个值来记录,sum存当前房间人数,ans维护最大值。

 

 

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <math.h>

using namespace std;

#define INF 0x3f3f3f3f
const int maxn = 1e6+5;

typedef long long LL;

int v[maxn];

int main()
{
    int n, m, sum, ans;
    char ch[5];

    while(scanf("%d", &n)!=EOF)
    {
        memset(v, 0, sizeof(v));
        sum = ans = 0;
        
        ///sum存当前房间里有多少个人
        ///ans存房间至少有多少个人
        for(int i=0; i<n; i++)
        {
            scanf("%s%d", ch, &m);
            if(ch[0]=='+')
            {
                v[m] ++;
                sum ++;
                ans = max(ans, sum);
            }
            else
            {
                if(!v[m]) ans++;///若v[m]==0,则证明它是在图书馆系统未开放前进去的
                else sum--;///若v[m]有值,则反之
            }
        }

        printf("%d\n", ans);

    }
    return 0;
}
View Code

 

posted on 2016-08-16 10:02  不忧尘世不忧心  阅读(265)  评论(0编辑  收藏  举报