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;
}
题 意:一个图书馆,有个程序,记录了进来和出去的人,但是开这个程序之前可能房间内还有一些人,问这个房间至少能容纳多少人才行(即房间内的最大人数)
思 路:一个栈的模拟题,'-'表示栈,‘+’表示入栈,同时在开始计算之前房间内就已经有人,所以‘-’可以在‘+’之前。
设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;
}
相关文章推荐
- Javascript SHA-1:Secure Hash Algorithm
- [转]可视化的数据结构和算法
- 统计文件中不小于某一长度的单词的个数(泛型算法实现)
- 使用他人的MD5编码类,修改形成密码串
- Extracting Structured Data from Web Pages
- (译)Cocos2d_for_iPhone_1_Game_Development_Cookbook:1.13使用CCTexture2DMutable调换调色盘
- Java中3DES加密
- Refactoring Notes-Refactoring Methods(3)
- 图书馆管理程序~~不过貌似功能!!有空再修修
- trainging contest#2(2011成都现场赛)I BY Hyoga
- C/C++头文件包含内容概览
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- 堆栈的应用(1) 平衡符号 C++实现
- 程序员编程艺术第一章、左旋转字符串
- 程序员编程艺术:第三章续、Top K算法问题的实现
- 程序员编程艺术:第四章、现场编写类似strstr/strcpy/strpbrk的函数
- 十四、第三章再续:快速选择SELECT算法的深入分析与实现
- 程序员编程艺术:第七章、求连续子数组的最大和
- 程序员编程艺术:第八章、从头至尾漫谈虚函数