您的位置:首页 > 编程语言 > Go语言

Codeforces 567 B. Berland National Library

2015-08-10 10:56 309 查看
题目链接:http://codeforces.com/problemset/problem/567/B

题        意:一个图书馆,有个程序,记录了进来和出去的人,但是开这个程序之前可能房间内还有一些人,问这个房间至少能容纳多少人才行(即房间内的最大人数)

思        路:一个栈的模拟题,'-'表示栈,‘+’表示入栈,同时在开始计算之前房间内就已经有人,所以‘-’可以在‘+’之前。

                  设ans和now,now表示当前程序记录下的房间人数。ans表示答案

                  遇到+,表示进来了一个人,那么标记一下,然后now++,再更新一下ans取最大

                  如果遇到-,如果之前被标记了,表示是程序开始后才进来的,所以直接now--就行

                 如果之前没有被标记,表示程序开启之前就已经进来了,所以直接在ans上面加上之前已经存在的人的数量,所以ans++。

代码如下:#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <queue>
using namespace std;
typedef __int64 LL;
int vis[10000005];
int main()
{
int n;
while( scanf ( "%d",&n ) != EOF )
{
int ans = 0, now = 0;
memset( vis, 0, sizeof(vis) );
for( int i = 0; i < n; i ++ )
{
char ch;
int m;
scanf ( " %c %d", &ch, &m );
if( ch == '-' )
{
if( vis[m] == 0 ) {ans++;}//表示在之前就已经在房间内的读者
else {now--;vis[m]=0;}//一个读者离开,则房间内的人数减少(他还可以再来,vis要归0)
}
if( ch == '+' )
{
vis[m] = 1;
now++;
if( now > ans ) ans = now;//如果房间内的最大人数变大则更新数据
}
}
printf("%d\n",ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces algorithm