codeforce round42-B

B. Students in Railway Carriage
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are nn 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 aa student-programmers and bb student-athletes. Determine the largest number of students from all a+ba+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).

Input

The first line contain three integers nnaa and bb (1n21051≤n≤2⋅1050a,b21050≤a,b≤2⋅105a+b>0a+b>0) — total number of seat places in the railway carriage, the number of student-programmers and the number of student-athletes.

The second line contains a string with length nn, consisting of characters "." and "*". The dot means that the corresponding place is empty. The asterisk means that the corresponding place is occupied by the jury member.

Output

Print the largest number of students, which you can put in the railway carriage so that no student-programmer is sitting next to a student-programmer and no student-athlete is sitting next to a student-athlete.

Examples
input
Copy
5 1 1
*...*
output
Copy
2
input
Copy
6 2 3
*...*.
output
Copy
4
input
Copy
11 3 10
.*....**.*.
output
Copy
7
input
Copy
3 2 3
***
output
Copy
0
Note

In the first example you can put all student, for example, in the following way: *.AB*

In the second example you can put four students, for example, in the following way: *BAB*B

In the third example you can put seven students, for example, in the following way: B*ABAB**A*B

The letter A means a student-programmer, and the letter B — student-athlete.


题意:给你三个整数n,a,b,和一个长度为n的字符串,a表示有a个学生程序员,b表示有b个学生运动员,字符串只包含两种字符 * 和 . 分别代表无座位和有座位,你的任务是给这些学生安排座位,要求是程序员与运动员必须隔开坐,例如(ABA或A.A或B.B)

题解:先用数组存下来连续的空座位数量,然后分情况模拟就行了,如果是奇数座位优先安排ab中人最多的,偶数随便,细节决定AC。

#include<stdio.h>
#include<stack>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<queue>
#include<map>
#include<set>
#include<queue>
using namespace std;
#define LL long long int 


int main()
{
	int n;
	int a, b;
	char st[200005];
	while (cin >> n>>a>>b) {
		scanf("%s", st);
		int cnt = 0;
		int s[10006];
		int k = 0;
		for (int i = 0;i < n;i++) {
			if (st[i] == '*') {
				if(cnt!=0)
				s[k++] = cnt;
				cnt = 0;
			}
			else {
				cnt++;
			}
		}
		if (cnt != 0) {
			s[k++] = cnt;
			cnt = 0;
		}
		sort(s, s + k);
		int ans = 0;
		for (int i = 0;i < k;i++) {
			if (s[i] & 1) {
				int tmp = s[i] / 2;
					if (a >= tmp) {
						ans += tmp;
						a -= tmp;
					}
					else {
						ans += a;
						a = 0;
					}
					if (b >= tmp) {
						ans += tmp;
						b -= tmp;
					}
					else {
						ans += b;
						b = 0;
					}
					if (a >= b) {
						if (a >= 1) {
							ans++, a--;
						}
						else if (b >= 1) {
							ans++, b--;
						}
					}
					else {
						if (b >= 1) {
							ans++, b--;
						}
						else if (a >= 1) {
							ans++, a--;
						}
					}

			}
			else {
				int tmp = s[i] / 2;
				if (a >= tmp) {
					ans += tmp;
					a -= tmp;
				}
				else {
					ans += a;
					a = 0;
				}
				if (b >= tmp) {
					ans += tmp;
					b -= tmp;
				}
				else {
					ans += b;
					b = 0;
				}
			}
		}
		cout << ans << endl;

	}
	return 0;
}


猜你喜欢

转载自blog.csdn.net/swust_zeng_zhuo_k/article/details/80112992