Codeforces Round #386 (Div. 2) 746D Green and Black Tea 【Analog】

Topic Portal: Click to open the link

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



The meaning of the question: a person wants to drink tea, there are a total of n tea bags: divided into two types of tea: G and B, the quantities are: a and b. This person wants to drink all the n tea bags at once. But you can't drink the same tea more than k times in a row: ask if it's possible for you to drink all of these teas. If possible, output the sequence of drinking tea.


Idea: Let's first find the smaller one of G and B: use it as a divider.

Let's take an example: if G is less.

get a sequence

     0   1  0   1 0   1 0  1 0 1  0

In this sequence, 1 means that there is a bag of G tea bags at this position. 0 means that B tea bags can be placed in this position, and the number is unknown.

Then we place the B teabag from left to right in this sequence to the 0 position. Get a new sequence if any element of this sequence is greater than k. proved impossible. If not, output as usual.

#include<bits/stdc++.h>
using namespace std;
intmain()
{
    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)///Place 1 position to separate tea bags
                {
                    if(num[i]==0)
                    {
                        num[i]++;
                    }

                }
                else
                {
                    if(ma>0)
                    {
                        num[i]++;///Place the tea bag at position 0
                    }

                    ma--;
                }
            }
        }

        int flag=1;
        for(int i=1; i<=mi*2+1; i++)
        {
            if(num[i]>k)///Detect possibility
            {
                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);///Output the letters separating tea bags
                }
                else
                {
                    for(int j=0;j<num[i];j++)
                    {
                        printf("%c",aa);///Output the letters filled with tea bags
                    }
                }
            }
        }
        printf("\n");

    }
    return 0;
}





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325983261&siteId=291194637