链接:https://vjudge.net/problem/CodeForces-962B
题目
There are n consecutive seat places in a railway carriage. Each place is either empty or occupied by a passenger.
The university team for the Olympiad consists of a student-programmers and b student-athletes. Determine the largest number of students from all a+b students, which you can put in the railway carriage so that:
- no student-programmer is sitting next to the student-programmer;
- and no student-athlete is sitting next to the student-athlete.
In the other words, there should not be two consecutive (adjacent) places where two student-athletes or two student-programmers are sitting.
Consider that initially occupied seat places are occupied by jury members (who obviously are not students at all).
题意
一群程序员(A类人群)和一群运动员(B类人群)一起坐地铁。
现在地铁上有n个位置,.
代表空位,*
代表有人坐。
众所周知,一山不容二虎,除非一公一母(doge)。程序员不想和程序员挨着,运动员也是一样。
现在他们想你求助,问你最多能有多少人搭乘地铁(看来他们并不想站着)?
思路
一开始没看懂题意,不过很快就get到点了。
这里我们贪心走一波就好了,每次都优先给人数多的那一方找座位.
从前向后遍历字符串,如果有座位先考虑人数多的一方能不能在这安排一个人,如果不行再看另一方有没有人。
如果另一方没人了就让仅剩的那一方隔着一个位置坐(例如A.A.A)。
废话不多说,伊丽莎白!!
代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,s1,s2,k=0;
string ss;
cin>>n>>s1>>s2;
cin>>ss;
for(int i=0; i<n; i++)
{
if(ss[i]=='.')
{
if(s1>=s2) //看一下哪边人多
{
if(ss[i-1]!='A'&&s1)
s1--,k++,ss[i]='A';
else if(ss[i-1]!='B'&&s2)
s2--,k++,ss[i]='B';
}
else
{
if(ss[i-1]!='B'&&s2)
s2--,k++,ss[i]='B';
else if(ss[i-1]!='A'&&s1)
s1--,k++,ss[i]='A';
}
}
}
cout<<k<<endl;
}
又是舒适的一天,溜了溜了~~