Codeforces Round #386(Div. 2)D. Green and Black Tea【思维+构造】

来源:互联网 发布:淘宝详情页英文素材 编辑:程序博客网 时间:2024/06/01 19:39

D. Green and Black Tea
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Input

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.

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.

Examples
Input
5 1 3 2
Output
GBGBG
Input
7 2 2 5
Output
BBGBGBB
Input
4 3 4 0
Output
NO

题目大意:

构造一个长度为N的字符串,其中一共有两种字符,B/G.一共有a个G,b个B,需要保证答案字符串没有超过K个连续的相同字符。


思路:


我们将数量较多的字符每个部分都用K个相同的部分来组成,然后我们将数量较少的字符往里边插。


Ac代码:

#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int n,k,a,b;char ans[10000600];int main(){    while(~scanf("%d%d%d%d",&n,&k,&a,&b))    {        char aa='G';        char bb='B';        if(a>b)        {            swap(a,b);            swap(aa,bb);        }        for(int i=0;i<n;i++)        {            ans[i]=bb;        }        int flag=0;        for(int i=k;i<n;i+=k+1)        {            if(a>0)            {                ans[i]=aa;                a--;            }            else            {                flag=1;            }        }        if(flag==0)        {            for(int i=0;i<n;i++)            {                if(a==0)break;                if(ans[i]==aa||(i>0&&ans[i-1]==aa)||(i+1<n&&ans[i+1]==aa))continue;                ans[i]=aa;                a--;            }            for(int i=0;i<n;i++)            {                printf("%c",ans[i]);            }            printf("\n");        }        else printf("NO\n");    }}




0 0
原创粉丝点击