Codeforces Round #386 (Div. 2)D. 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 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.

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包茶,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);
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值