构造 Codeforces Round #Pi (Div. 2) B. Berland National Library

题目传送门

 1 /*
 2     题意:给出一系列读者出行的记录,+表示一个读者进入,-表示一个读者离开,可能之前已经有读者在图书馆
 3     构造:now记录当前图书馆人数,sz记录最小的容量,in数组标记进去的读者,分情况讨论一下
 4 */
 5 /************************************************
 6 * Author        :Running_Time
 7 * Created Time  :2015-8-6 0:23:37
 8 * File Name     :B.cpp
 9  ************************************************/
10 
11 #include <cstdio>
12 #include <algorithm>
13 #include <iostream>
14 #include <sstream>
15 #include <cstring>
16 #include <cmath>
17 #include <string>
18 #include <vector>
19 #include <queue>
20 #include <deque>
21 #include <stack>
22 #include <list>
23 #include <map>
24 #include <set>
25 #include <bitset>
26 #include <cstdlib>
27 #include <ctime>
28 using namespace std;
29 
30 #define lson l, mid, rt << 1
31 #define rson mid + 1, r, rt << 1 | 1
32 typedef long long ll;
33 const int MAXN = 1e6 + 10;
34 const int INF = 0x3f3f3f3f;
35 const int MOD = 1e9 + 7;
36 bool in[MAXN];
37 
38 int main(void)    {     //Codeforces Round #Pi (Div. 2) B. Berland National Library
39     int n;  scanf ("%d", &n);
40     memset (in, false, sizeof (in));
41     int now = 0, sz = 0;
42     for (int i=1; i<=n; ++i)    {
43         char s[2];    int x;  scanf ("%s%d", &s, &x);
44         if (s[0] == '+')  {
45             now++;  in[x] = true;
46             if (now > sz)   sz++;
47         }
48         else if (s[0] == '-')   {
49             if (now == 0)   {
50                 if (in[x])  in[x] = false;
51                 sz++;
52             }
53             else    {
54                 if (!in[x]) {       //是之前就进入图书馆的读者
55                     sz++;
56                 }
57                 else    {
58                     in[x] = false;  now--;
59                 }
60             }
61         }
62     }
63 
64     printf ("%d
", sz);
65 
66     return 0;
67 }
编译人生,运行世界!
原文地址:https://www.cnblogs.com/Running-Time/p/4709181.html