B. Berland National Library---cf567B(set|模拟)

 题目链接:http://codeforces.com/problemset/problem/567/B

 题意:题目大意: 一个计数器, +号代表一个人进入图书馆, -号代表一个人出去图书馆. 给一个log, 问这个log中能知道的这个图书馆最小的可能容量是多少, log是片段, 在program运行前可能图书馆已经有人, 在运行后也

可能人还没出去?

方法一:模拟;

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <map>
#include <string>
using namespace std;
#define N 1000005
#define INF 0x3f3f3f3f
#define met(a, b) memset(a, b, sizeof(a))
typedef long long LL;

int n,  a[N], b[N];

char s[N][10];
int main()
{
    while(scanf("%d", &n) != EOF)
    {
        met(a, 0);
        met(b, 0);

        int ans = 0;

        for(int i=0; i<n; i++)
        {
            scanf("%s %d", s[i], &a[i]);
            if(s[i][0] == '+')
                b[a[i]] = 1;
            else if(b[a[i]] == 0)
                ans ++;///先求出原本里面有多少人;
        }

        int Max = ans;
        for(int i=0; i<n; i++)
        {
            if(s[i][0] == '+')
                ans ++;
            else
                ans--;
            Max = max(Max, ans);///再统计过程中的流通人量;
        }
        printf("%d
", Max);
    }
    return 0;
}
View Code

方法二:STL

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <vector>
#include <map>
#include <string>
#include <set>
using namespace std;
#define N 1000005
#define INF 0x3f3f3f3f
#define met(a, b) memset(a, b, sizeof(a))
typedef long long LL;

int main()
{
    int n;
    while(~scanf("%d", &n))
    {
        set<int>s;
        int ans = 0;
        for(int i=1; i<=n; i++)
        {
            char ch[5]; int num;
            scanf("%s %d", ch, &num);
            if(ch[0] == '-' && s.find(num) == s.end() )
                ans++;
            else if(ch[0] == '-' && s.find(num)!=s.end() )
                s.erase(num);
            else
            {
                s.insert(num);
                int len = s.size();
                ans = max(ans, len);
            }
        }
        printf("%d
", ans);
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/zhengguiping--9876/p/5674949.html