DoorMan

本文介绍了一个关于夜店门卫如何通过调整进入顺序来平衡性别比例的问题。通过简单的算法实现,确保入场男女比例接近的同时,最大化入场人数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description

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

2

WMMMMWWMMMWWMW

Sample Output

8

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
using namespace std;
int max(int a,int b)
{
	return a>b?a:b;
}
int min(int a,int b)
{
	return a>b?b:a;
}
int main()
{
	int n,Max=0,Min=0;
	bool flag=0;
	cin>>n;
	string a;
	cin>>a;
	int m=0,w=0,s;
	for(int i=0;i<a.size();i++)
	{
		if(a[i]=='W')
			w+=1;
		if(a[i]=='M')
			m+=1;
		Min=min(w,m);
		Max=max(w,m);
		if(Max-Min>n+1)
		{
			flag=1;
			break;
		}
	}
	if(flag==0&&Max-Min<=n)
		s=a.size();
	else
		s=Min*2+n;
	cout<<s;
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值