codeforce 386(div2) D. Green and Black Tea

Innokentiy likes tea very much and today he wants to drink exactly n cups of tea. He would be happy to drink more but he had exactly n tea bags, a of them are green and b are black.

Innokentiy doesn't like to drink the same tea (green or black) more than k times in a row. Your task is to determine the order of brewing tea bags so that Innokentiy will be able to drink n cups of tea, without drinking the same tea more than k times in a row, or to inform that it is impossible. Each tea bag has to be used exactly once.

Input

The first line contains four integers nka and b (1 ≤ k ≤ n ≤ 105, 0 ≤ a, b ≤ n) — the number of cups of tea Innokentiy wants to drink, the maximum number of cups of same tea he can drink in a row, the number of tea bags of green and black tea. It is guaranteed thata + b = n.

Output

If it is impossible to drink n cups of tea, print "NO" (without quotes).

Otherwise, print the string of the length n, which consists of characters 'G' and 'B'. If some character equals 'G', then the corresponding cup of tea should be green. If some character equals 'B', then the corresponding cup of tea should be black.

If there are multiple answers, print any of them.

這邊比如a==0或b==0是比較容易判斷的。(兩個不能同時爲零,因爲n>=1)

a==b就是間隔的輸出。

這邊假設a>b,(與a<b的情況是一樣的),拿最優的情況就是a平均分成b+1份,如果有餘數的話就是某幾項要加1,判斷是否會超過k

然後就是模擬了;

 

AC代碼:

 

#include<stdio.h>
#include<iostream>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long int ll;
int main() {
	int i, j, k, a, b, n;//a--G, b--B
	scanf("%d%d%d%d", &n, &k, &a, &b);
	//for (i = 1; i <= n; i++)
	if (a == 0) {
		if (k >= b) {
			for (i = 1; i <= b; i++) {
				printf("B");
			}
		}
		else {
			printf("NO");
		}
	}
	else if (b == 0) {
		if (k >= a) {
			for (i = 1; i <= a; i++) {
				printf("G");
			}
		}
		else {
			printf("NO");
		}
	}
	else {
		if (a == b) {
			for (i = 1; i <= a; i++) {
				printf("GB");
			}
		}
		else if (a > b) {
			int mod = a%(b+1), t=a/(b+1);
			if (mod == 0) {
				if (t <= k) {
					for (i = 1; i <= b + 1; i++) {
						if (i != b + 1) {
							for (j = 1; j <= t; j++) {
								printf("G");
							}
							printf("B");
						}
						else {
							for (j = 1; j <= t; j++) {
								printf("G");
							}
						}
					}
				}
				else {
					printf("NO");
				}
			}
			else {
				if (t + 1 > k) {
					printf("NO");
					return 0;
				}
				for (i = 1; i <= mod; i++) {
					for (j = 1; j <= t + 1; j++) {
						printf("G");
					}
					printf("B");
				}
				for (i = mod + 1; i <= b + 1; i++) {
					if (i != b + 1) {
						for (j = 1; j <= t; j++) {
							printf("G");
						}
						printf("B");
					}
					else {
						for (j = 1; j <= t; j++) {
							printf("G");
						}
					}
				}
			}
		}
		else {

			
			int mod = b % (a + 1), t = b / (a + 1);
			if (mod == 0) {
				if (t <= k) {
					for (i = 1; i <= a + 1; i++) {
						if (i != a + 1) {
							for (j = 1; j <= t; j++) {
								printf("B");
							}
							printf("G");
						}
						else {
							for (j = 1; j <= t; j++) {
								printf("B");
							}
						}
					}
				}
				else {
					printf("NO");
				}
			}
			else {
				if (t + 1 > k) {
					printf("NO");
					return 0;
				}
				for (i = 1; i <= mod; i++) {
					for (j = 1; j <= t + 1; j++) {
						printf("B");
					}
					printf("G");
				}
				for (i = mod + 1; i <= a + 1; i++) {
					if (i != a + 1) {
						for (j = 1; j <= t; j++) {
							printf("B");
						}
						printf("G");
					}
					else {
						for (j = 1; j <= t; j++) {
							printf("B");
						}
					}
				}
			}
		}
	}
	return 0;
}
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章