CodeForces 567B Berland National Library 容器模拟

来源:互联网 发布:淘宝上如何捐赠旧衣物 编辑:程序博客网 时间:2024/05/17 22:56

原题: http://codeforces.com/contest/567/problem/B

题目:

Berland National Library
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Berland National Library has recently been built in the capital of Berland. In addition, in the library you can take any of the collected works of Berland leaders, the library has a reading room.

Today was the pilot launch of an automated reading room visitors’ accounting system! The scanner of the system is installed at the entrance to the reading room. It records the events of the form “reader entered room”, “reader left room”. Every reader is assigned a registration number during the registration procedure at the library — it’s a unique integer from 1 to 106. Thus, the system logs events of two forms:

“+ ri” — the reader with registration number ri entered the room;
“- ri” — the reader with registration number ri left the room.
The first launch of the system was a success, it functioned for some period of time, and, at the time of its launch and at the time of its shutdown, the reading room may already have visitors.

Significant funds of the budget of Berland have been spent on the design and installation of the system. Therefore, some of the citizens of the capital now demand to explain the need for this system and the benefits that its implementation will bring. Now, the developers of the system need to urgently come up with reasons for its existence.

Help the system developers to find the minimum possible capacity of the reading room (in visitors) using the log of the system available to you.

Input
The first line contains a positive integer n (1 ≤ n ≤ 100) — the number of records in the system log. Next follow n events from the system journal in the order in which the were made. Each event was written on a single line and looks as “+ ri” or “- ri”, where ri is an integer from 1 to 106, the registration number of the visitor (that is, distinct visitors always have distinct registration numbers).

It is guaranteed that the log is not contradictory, that is, for every visitor the types of any of his two consecutive events are distinct. Before starting the system, and after stopping the room may possibly contain visitors.

Output
Print a single integer — the minimum possible capacity of the reading room.

Sample test(s)
input
6
+ 12001
- 12001
- 1
- 1200
+ 1
+ 7
output
3
input
2
- 1
- 2
output
2
input
2
+ 1
- 1
output
1
Note
In the first sample test, the system log will ensure that at some point in the reading room were visitors with registration numbers 1, 1200 and 12001. More people were not in the room at the same time based on the log. Therefore, the answer to the test is 3.

思路:

图书馆的刷卡系统,记录进出信息,但是可能在机器启动前就有人进去了,关闭的时候也有肯能没出来,求图书馆至少要多大空间。

如果当前求得的最大值为x,当前容量为an,那么我们每次放东西,an++,x要去x和an++的最大值。
每次拿东西出来的时候,如果出来的以前在里面,直接拿出来就是,如果没出显过,我们可以理解是我们最初就开机的时候加进去的,因为他们一直存在,所以当前状态下总容量最大的时候他们肯定还里面,所以总容量加上他们就行了。

这里我用vector模拟图书馆的状态求值。

代码:

#include <iostream>#include"string.h"#include"cstdio"#include"stdlib.h"#include"algorithm"#include"vector"using namespace std;typedef __int64 lint;int main(){    vector <int> vi;    vector <int> ::iterator it;    int t;    char s[10];    int temp;    while(scanf("%d",&t)!=EOF)    {        int ans=0;        while(t--)        {            scanf("%s",s);            scanf("%d",&temp);            if(s[0]=='+')            {                vi.push_back(temp);                int an=vi.size();                ans=max(ans,an);            }            else if(s[0]=='-')            {                int flag=0;                for(it=vi.begin(); it!=vi.end(); it++)                {                    if(*it==temp)                    {                        vi.erase(it);                        flag=1;                        break;                    }                }                if(flag==0)                {                    ans++;                }            }        }        printf("%d\n",ans);    }    return 0;}
0 0