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.
The first line contains four integers n, k, a 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 that a + b = n.
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.
5 1 3 2
GBGBG
7 2 2 5
BBGBGBB
4 3 4 0
NO
题意:有n包茶,a包绿茶,b包黑茶,每种茶最多连续喝k次,问能把n包茶喝完的方案,如果没有输出NO。
思路:先喝多的,尽量喝,直到ab相等或者喝了k杯,如果ab相等那不用管k了,轮流喝完ok,如果a还不等于b,那原先多的还是多,先将另一种茶喝一杯,然后再喝另一种。
使得两种茶趋向相等,相等就可以解决了,如果出现要喝一种茶但茶已经被喝光了,输出NO。
#include <bits/stdc++.h> using namespace std; #define ll long long int #define maxn 100010 char s[maxn]; int main() { int n, k, a, b, p = 0, i; scanf("%d %d %d %d", &n, &k, &a, &b); bool flag = 1, c; if(a>b){ int tmp = min(k, a-b); for(i = 1;i <= tmp;i++){ a--; s[p++] = 'G'; } c = 0; }else if(b>a){ int tmp = min(k, b-a); for(i = 1;i <= tmp;i++){ b--; s[p++] = 'B'; } c = 1; }else { for(i = 1;i <= a*2;i++) s[p++] = ((i%2==1)?'G':'B'); a = b = 0; } while(a||b){ if(a==b){ if(c) for(i = 1;i <= a*2;i++) s[p++] = ((i%2==1)?'G':'B'); else for(i = 1;i <= a*2;i++) s[p++] = ((i%2==1)?'B':'G'); break; } if(c){ //for(i = 1;i <= min(k, a-b);i++){ if(a==0){ flag = 0; break; } a--; s[p++] = 'G';//printf("%d %d %d\n", c, k, b-a); int tmp = min(k, b-a); for(i = 1;i <= tmp;i++){ b--; s[p++] = 'B'; } //} c = 1; }else { //printf("%d\n", c); if(b==0){ flag = 0; break; } //for(i = 1;i <= min(k, b-a);i++){ b--; s[p++] = 'B'; //} int tmp = min(k, a-b); for(i = 1;i <= tmp;i++){ a--; s[p++] = 'G'; } c = 0; } } if(!flag) printf("NO\n"); else { s[p] = '\0'; printf("%s\n", s); } }