• 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
  • 相关阅读:
    关于Tomcat的那些事儿(一)
    关于Spring的问题(一)
    关于Spring的@RequestBody的使用
    关于Eclipse切换所有分支
    关于使用BetterIntelliJ激活IntelliJ IDEA
    关于Elasticsearch的问题(一)
    关于Elasticsearch那些事
    关于LRU算法及Java实现
    自定义滚动条
    创建型模式之建造者模式
  • 原文地址:https://www.cnblogs.com/zhengguiping--9876/p/5674949.html
Copyright © 2020-2023  润新知