BNU 26476 Doorman【NC】

链接:


Doorman

1000ms
65536KB
64-bit integer IO format:  %lld      Java class name:  Main
Font Size:   
Type: 
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •                    
  • The doorman Bruno at the popular night club Heaven is having a hard time fulfilling his duties. He was told by the owner that when the club is full, the number of women and men let into the club should be roughly the same. When the night club opens, people wanting to enter the club are already lined up in a queue, and Bruno can only let them in one-by-one. He lets them in more-or-less in the order they are lined up. He can however decide to let the second person in the queue cut the line and slip into the club before the person in front. This will no doubt upset the person first in line, especially when this happens multiple times, but Bruno is quite a big guy and is capable of handling troublemakers. Unfortunately though, he is not that strong on mental calculations under these circumstances. He finds keeping track of the difference of the number of women and number of men let into the club a challenging task. As soon as the absolute difference gets too big, he looses track of his counting and must declare to the party people remaining in the queue that the club is full.

    Input

    The first line of input contains a positive integer  X<100  describing the largest absolute difference between the number of women and number of men let into the club, that Bruno can handle. The second line contains a string consisting solely of the characters ’W’ and ’M’ of length at most  100 , describing the genders of the people in the queue, in order. The leftmost character of the string is the gender of the person first in line.

    Output

    The maximum number of people Bruno can let into the club without loosing track of his counting. You may assume that the club is large enough to hold all the people in the queue.

    Sample Input

    1
    MWWMWMMWM

    Sample Output

    9

    Source



    题意:


          第一行 N 表示能够容忍的不能配对的人数
         第二行一条 100 个字符以内的字符串'W'代表女人, 'M'代表男人


          看门人一次只让一个人进去,男女尽量配对,如果不配对的人数超过了 N
          那么看们人就分不清了,此时他就会宣布俱乐部人满,不再让后面的人进入
          输出能进去的最多的人数。


    思路:


    每次进入一个人就判断一次男女不配对的人数,直到不合法。

    注意:关于分不清的时候的跳出判断情况。
          要一直判断到男女之差 > N+1
          因为即便是最多只能容许 N 个人没有配对,查找到了第 N+1 个人不能配对
          那么如果查到下一个人可以和前面的配对,那么就可以新增加两个人


          也就是说:
          2
          MMMW 的结果是 4

    总结:很水的一题了,比赛的时候开始没看清楚题意,见到过的人比较多,随便YY了下 WA
          然后再看题,但是到了男女只差正好是 N+1 的时候跳出判断的 WA。
          然后懒得搞了,继续看其他的题目,Orc 一个人一直死磕这题。
          最后我又返回来写这题,让 Orc 找了组数据,测试过了,然后我说交了啊!Orc 绝望的同意了,当时差不多所有的队伍都过了
          吧。
          然后终于 AC 了,但是此时已经过了两个小时了。简单的题目做的慢,难的题目搞不出,最终惨淡的逃不过被虐的命运。。。

    我想着等到正式比赛的时候,应该对于难的题目,在那种压力下,就算是那种算法我们平时有过研究,但是应该也无法快速准确的写出来,所以关键就在于快速准确的 AC 简单的题目了。

    code:

    /**
    题意:第一行 N 表示能够容忍的不能配对的人数
          第二行一条 100 个字符以内的字符串'W'代表女人, 'M'代表男人
    
          看门人一次只让一个人进去,男女尽量配对,如果不配对的人数超过了 N
          那么看们人就分不清了,此时他就会宣布俱乐部人满,不再让后面的人进入
          输出能进去的最多的人数。
    
          注意:关于分不清的时候的跳出判断情况。
                要一直判断到男女之差 > N+1
                因为即便是最多只能容许 N 个人没有配对,查找到了第 N+1 个人不能配对
                那么如果查到下一个人可以和前面的配对,那么就可以新增加两个人
    
                也就是说:
                2
                MMMW 的结果是 4
    
    */
    #include<string>
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    
    char str[110];
    int n;
    int main()
    {
        while(scanf("%d", &n) != EOF)
        {
            scanf("%s", &str);
            int n1;
            int n2;
            int ans = 0;
            n1 = n2 = 0;
            int len = strlen(str);
            int flag = 0;
            int Min, Max;
            for(int i = 0; i < len; i++)
            {
                if(str[i] == 'M') n1++;
                if(str[i] == 'W') n2++;
                Min = min(n1, n2); //依次记录一次
                Max = max(n1, n2);
                if(Max-Min > (n+1)) //如果男女之差比能准许的 N 要大 2,那么守门人就分不清了,马上宣布人满,不管后面排队的
                {
                    flag = 1; //标记
                    break;
                }
            }
            if(flag == 0 && Max-Min <= n)  //如果没有标记,全部排队的人数也合法:男女之差不超过 N
                ans = len;
            else //如果标记了,最大限度的让人进去,男女之差正好为 N
                ans = Min*2+n; //当然也包含了没有标记,男女之差 > N 的情况
            printf("%d\n", ans);
        }
        return 0;
    
    }
    


    • 0
      点赞
    • 0
      收藏
      觉得还不错? 一键收藏
    • 0
      评论

    “相关推荐”对你有帮助么?

    • 非常没帮助
    • 没帮助
    • 一般
    • 有帮助
    • 非常有帮助
    提交
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值