题目传送门:点击打开链接
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 exactlyn tea bags, a of them are green andb 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 drinkn cups of tea, without drinking the same tea more thank 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 andb (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.
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个茶包:分为两种茶:G和B,数量分别为:a和b。这个人要一次把这n个茶包全部喝掉。但同一种茶不能连续喝超过k次:问你全喝掉这些茶是否可能。如果可能,则输出喝茶的序列。
思路: 我们先把G和B中数量较小的那个找出来:将其作为分隔物。
我们举个例子:假如G较少。
得出一个数列
0 1 0 1 0 1 0 1 0 1 0
这个数列中,1代表这个位置有一包G茶包。0代表这个位置可以放置B茶包,数量未知。
然后我们将B茶包从这个数列从左到右放置到0位置处。得到一个新数列,如果这个数列中有任意一个元素大于k的话。就证明不可能。如果没有,则照例输出。
#include<bits/stdc++.h>
using namespace std;
int main()
{int num[100005];int n,k,g,b;while(~scanf("%d %d %d %d",&n,&k,&g,&b)){memset(num,0,sizeof(num));int mi=min(g,b);int ma=max(g,b);char ii,aa;if(g>=b){aa='G';ii='B';}else{aa='B';ii='G';}while(ma>0){
// printf("ma==%d\n",ma);for(int i=1; i<=mi*2+1; i++){if(i%2==0)///放置1位置分隔茶包{if(num[i]==0){num[i]++;}}else{if(ma>0){num[i]++;///放置0位置茶包}ma--;}}}int flag=1;for(int i=1; i<=mi*2+1; i++){if(num[i]>k)///检测可能性{flag=0;break;}}// for(int i=1;i<=mi*2+1;i++)
// {
// printf("%d ",num[i]);
// }if(flag==0){printf("NO\n");continue;}else{for(int i=1; i<=mi*2+1; i++){if(i%2==0){printf("%c",ii);///输出分隔茶包字母}else{for(int j=0;j<num[i];j++){printf("%c",aa);///输出填充茶包字母}}}}printf("\n");}return 0;
}