题意:一个地铁有n座位,现在有a类参加算法学生,和b类参加运动比赛的学生。不能让相同的类的学生相邻而坐。也就是a不能和a坐,b不能和b坐。输入一串字符,.代表空座位,*代表有人已经坐了。现在让你求出可以运输的学生的最大人数。
题解:贪心 先让a,b多的那一类人先坐一个,然后轮流坐最好,如果只有一类学生,那就隔开坐。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,a,b,ans=0;
string s;
cin>>n>>a>>b>>s;
for(int i=0; i<s.size(); i++)
{
if(s[i]=='.')
{
if(a>=b)
{
if(s[i-1]!='a'&&a)
s[i]='a',ans++,a--;
else if(s[i-1]!='b'&&b)
s[i]='b',ans++,b--;
}
else
{
if(s[i-1]!='b'&&b)
s[i]='b',ans++,b--;
else if(s[i-1]!='a'&&a)
s[i]='a',ans++,a--;
}
}
}
cout<<ans<<endl;
return 0;
}