CodeForces - 567B Berland National Library

来源:互联网 发布:我心伤悲 莫知我哀赏析 编辑:程序博客网 时间:2024/05/18 01:46

题目大意:有一个房间,+ 表示进,- 表示出,后面的数字代表编号,问该房间最少能同时容纳多少人。

解题思路:用 now 统计当前人数,tot 表示历史最多人数,每改变一人就比较一次,注意 - 时会有两种情况,一开始不知道如何处理本来就在房间内的人,卡了很久,心塞

#include<iostream> #include<cstdio>#include<string.h>#include<stdlib.h>#include<cmath>using namespace std;int r[1000010];int main() {    int n;    while(scanf("%d", &n) != EOF) {        memset(r, 0, sizeof(r));        char c;        int tag;        int tot = 0,now = 0;        for (int i = 0; i < n; i++) {            getchar();            c = getchar();            scanf("%d", &tag);            if (c == '+') {                r[tag] = 1;                now++;            }            if (c == '-') {                if (r[tag] == 1) {                    r[tag] = 0;                    now--;                }                else if (r[tag] == 0) tot++;            }        tot = max (tot, now);        }        printf("%d\n", tot);    }    return 0; }
1 0
原创粉丝点击