codefroces 746D Green and Black Tea

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 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 ≤ 1050 ≤ 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.

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
题意:就是给定G和B共有n个,其中G有a个,B有b个,让你确定一种序列是的连着的B不超过k个,同理连着的G也不能超过K个。
思路:构造,构造。把数量少的字母插入到数量较多的字符串里面。刚开始我们就每隔k个插入一个字母,最后如果还有剩余的,我们就一个一个的插入就好了,具体的思路看代码
#include <iostream>
#include <cstdio>
#include <map>
#include <cmath>
#include <vector>
#include <queue>
#include <set>
#include <algorithm>
#include <cstring>

using namespace std;

const int max_n = 100005;
int n,k,a,b;
char s[max_n];

int main() {
    cin >> n >> k >> a >> b;
    char B='B',G='G';
    if(a > b){//确定a一顶小于b,把a插入到b里面就可以了
        swap(a,b);
        swap(G,B);
    }
    for(int i = 0; i< n;i++){/初始化原始序列
        s[i] = B;
    }
    for(int i = k;i < n;i+=k+1){//每隔k个插入一个a,如果发现a完之后b剩余的还比K多输出NO.
        if(a == 0){
            printf("NO\n");
            return 0;
        }
        s[i] = G;
        a--;
    }
    for(int i = 0;i < n&& a>0 ;i++){//间隔插入a
        if(s[i] == G||(i > 0&&s[i-1] == G)||((i+1)<n&&s[i+1]==G)){
            continue;
        }
        s[i] = G;
        a--;
    }
    cout << s;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值