Students in Railway Carriage CodeForces - 962B

Students in Railway Carriage

题目链接:CodeForces - 962B
题意:一排座位,'.'表示空位, '*'表示座位有人, 一共n个座位, 现在有a个A, b个B, 要求A不能和A靠着,B不能和B靠着, 问做多能安排下多少个人;
当连续座位数是奇数时按ABABA样式做(A是数量大的那一类, 这样就能多坐下一个人), 偶数就无所谓了(两类人坐下的数量相同);
其实不管连续座位数是奇还是偶, 我们只管安排人数多的一类先做下, 这样再顺着来,那么是不是先要求出每个连续空座区间呢?
并不需要的, 这样想, 只要遇到空位, 就安排人数多的下坐下就好了, 并且该座位的前一个不是同类, 如果是同类, 那么安排另一类的坐下;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char s[200010];
int seat[200010];
bool cmp(int a, int b){
	return a>b;
}
int main(){
	int n, a, b, ans=0;
	scanf("%d%d%d", &n, &a, &b);
	scanf("%s", s+1);
	s[0]='#';
	for(int i=1; i<=n; i++){
		if(a==0&&b==0) break;
		if(s[i]=='.'){
			if(a>b){
				if(s[i-1]!='A') s[i]='A', a--, ans++;
				else if(b>0) s[i]='B', b--, ans++;
			}
			else{
				if(s[i-1]!='B') s[i]='B', b--, ans++;
				else if(a>0) s[i]='A', a--, ans++;
			}
		}
	}
	printf("%d\n", ans);
	return 0;
} 


猜你喜欢

转载自blog.csdn.net/sirius_han/article/details/80341332