当前位置: 首页 > 工具软件 > Doorman > 使用案例 >

Doorman

李鸿
2023-12-01

题目描述
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.
输入
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.
输出
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.
输入输出样例
样例输入 #1
2
WMMMMWWMMMWWMW
样例输出 #1
8

【思路分析】

按照顺序进入,统计w和m的数量,由于门卫的功能可以使得m和w的数量最多相差x+1

例如样例中WMMMM并不违反,可以通过交换顺序变为MMWMM

如果队伍中都可以完成配对,则最多的人数就是队伍的长度,如果不能所有人都进去,最大的容量就是m与w中小的那个都有配对且加上x

【代码】

#include<bits/stdc++.h>
using namespace std;
int x;
char human[110];
int main()
{
    int w=0,m=0,flag=0;
    cin>>x>>human;
    int len=strlen(human);
    for(int i=0;i<len;i++)
    {
        if(human[i]=='W')
        w++;
        else
        m++;
        if(fabs(w-m)>x+1)
        {
            flag=1;
            break;
        }
    }
    if(!flag&&fabs(w-m)<=x+1)
    cout<<len;
    else
    cout<<min(w,m)*2+x;
    return 0;
}

 类似资料:

相关阅读

相关文章

相关问答