cf #314 B. Berland National Library (模拟)

给出一个图书馆人员进出情况,问图书馆满足题意的最小容量是多少。

注意在初始之前图书馆里面可能就有人了,也就是说不是所有进入图书馆的人都会被给出。

我的做法是先统计出图书馆里面初始的人数,开一个布尔数组,初始全为false,如果一个人标记为 false  而且从 图书馆里出来了,就说明这个人初始是在图书馆里的。

然后就正常模拟,图书馆的人数由初始的和后来的两部分组成。

 1/*************************************************************************
 2	> File Name: code/cf/#314/B.cpp
 3	> Author: 111qqz
 4	> Email: rkz2013@126.com 
 5	> Created Time: 2015年08月06日 星期四 00时23分26秒
 6 ************************************************************************/
 7
 8#include<iostream>
 9#include<iomanip>
10#include<cstdio>
11#include<algorithm>
12#include<cmath>
13#include<cstring>
14#include<string>
15#include<map>
16#include<set>
17#include<queue>
18#include<vector>
19#include<stack>
20#define y0 abc111qqz
21#define y1 hust111qqz
22#define yn hez111qqz
23#define j1 cute111qqz
24#define tm crazy111qqz
25#define lr dying111qqz
26using namespace std;
27#define REP(i, n) for (int i=0;i<int(n);++i)  
28typedef long long LL;
29typedef unsigned long long ULL;
30const int inf = 0x7fffffff;
31const int N=1E2+5;moni
32int n;
33int r[N];
34bool vis[1000060];
35char ch[N];
36int main()
37{
38    cin>>n;
39    char cmd;
40    int mx = -1;
41    int cur = 0;
42    int beforein = 0;
43    memset(vis,false,sizeof(vis));
44    for ( int i = 0 ; i < n ; i++)
45    {
46
47	cin>>ch[i]>>r[i];
48	if (ch[i]=='+')
49	{
50	    vis[r[i]] = true;
51	}
52	if (ch[i]=='-')
53	{
54	    if (!vis[r[i]])
55	    {
56		beforein++;
57	    }
58	    vis[r[i]] = false;
59	}
60    }
61    mx = beforein;
62    memset(vis,false,sizeof(vis));
63   // cout<<"beforein:"<<beforein<<endl;
64    for ( int i = 0 ; i <n ; i++ )
65    {
66	if (ch[i]=='+')
67	{
68	    cur++;
69	    vis [r[i]] =true;
70	}
71	else
72	{
73	    if (vis[r[i]])
74	    {
75//	    if (vis[1]) cout<<"fuffffffffff"<<endl;
76		cur--;
77//		cout<<"r[i]:"<<r[i]<<endl;
78		vis[r[i]] = false;
79	    }
80	    else
81	    {
82		beforein--;
83	    }
84	}
85//	cout<<"cur:"<<cur<<" beforein:"<<beforein<<endl;   
86	if (cur+beforein>mx)
87	{
88	    mx = cur + beforein;
89	}
90
91    }
92    cout<<mx<<endl;
93	return 0;
94}