Berland National Library codeforces 567B(模拟)

http://codeforces.com/problemset/problem/567/B

题意:图书馆有一个程序,可以记录学生的进出情况,‘+‘表示进入,‘-‘表示出去,字符后面的数字表示学生的编号。在这个程序尚未启动前,还有一些同学进的消息没有被记录下来。现在问你这个图书馆至少得容纳多少学生。

分析:用sum和ans两个值来记录,sum存当前房间人数,ans维护最大值。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <stack>
#include <math.h>

using namespace std;

#define INF 0x3f3f3f3f
const int maxn = 1e6+5;

typedef long long LL;

int v[maxn];

int main()
{
    int n, m, sum, ans;
    char ch[5];

    while(scanf("%d", &n)!=EOF)
    {
        memset(v, 0, sizeof(v));
        sum = ans = 0;

        ///sum存当前房间里有多少个人
        ///ans存房间至少有多少个人
        for(int i=0; i<n; i++)
        {
            scanf("%s%d", ch, &m);
            if(ch[0]==‘+‘)
            {
                v[m] ++;
                sum ++;
                ans = max(ans, sum);
            }
            else
            {
                if(!v[m]) ans++;///若v[m]==0,则证明它是在图书馆系统未开放前进去的
                else sum--;///若v[m]有值,则反之
            }
        }

        printf("%d\n", ans);

    }
    return 0;
}

时间: 2024-11-08 10:49:55

Berland National Library codeforces 567B(模拟)的相关文章

CodeForces 567B Berland National Library(模拟)(简单)

B. Berland National Library time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Berland National Library has recently been built in the capital of Berland. In addition, in the library you can t

CodeForces 567B Berland National Library

Description 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

构造 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

Codeforces-B. Berland National Library(模拟)

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 roo

Codeforces Round #Pi (Div. 2)——set——Berland National Library

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 roo

D - Berland National Library

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 roo

CodeForces 567B Berland National Library hdu

这类题一个操作增加多少,一个操作减少多少,求最少刚开始为多少,在中途不会出现负值,模拟一遍,用一个数记下最大的即可 1 #include<cstdio> 2 #include<cstring> 3 4 const int HASH=57; 5 6 int n,num[110],head[HASH],next[110]; 7 8 void insert(int s) 9 { 10 int h=num[s]%HASH; 11 int u=head[h]; 12 while(u) u=n

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

思路:对于这道题,一开始并没有什么思路,但后来想了下,其实就是维护一个集合,每一个人都是不同的元素,满足了集合的互异性,而要求这个图书馆最小的容纳量,其实就是求这个集合的最大的元素量,假设在某个时刻集合里存在M个元素,是集合从开始到结束出现过的元素个数的最大值,那么就是这个图书馆的最小容纳量,如果最小容纳量比M小,那怎么容得下M个人?对于+,   他之前肯定是没进或者之前出来股,无论怎样,都要加进集合. 对于一个集合的操作,在cf种时间为上的比赛,自然选用stl的set,如果各位有更好的思路或实

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

这道题我一开始想了很久,但是fst 了.而且赛后想的也不是很清楚,所以有必要把它单独分出来讲清楚. 题意: 现在在log中有n条记录.然后每一条记录都写成:"+ri"或是"-ri"的形式.(其中+是代表有人进入,-代表有人出去)ri代表的是进入人的号码. 然后问你房间中最多同时有几个人存在. 思路: 我们需要两个变量max与sum,其中max是记录房间的最大容量的,sum是表示当前还有多少空位. 1)如果一个人进来了,如果当前没有空位的话,那么就max++,使房间容