codeforces 746 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 ntea 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 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

题目大意:有红茶a包,黑茶b包,a+b=n,构造出一种方案使每种茶连续的数量不超过k,如果不存在合法方案就输出NO

题解:构造

这道题首先要考虑分成将两种茶分成了多少段。t=max(a/k,b/k)

然后根据段数计算出每段中a,b的数量,注意不能超过k,也不能小于1.

但是如果直接这么构造t段,最终剩余的a,b可能超过k,所以在构造的过程中我们要根据计算的剩余情况,更改每段中a,b的值。

#include<iostream>    
#include<cstdio>    
#include<cstring>    
#include<algorithm>    
using namespace std;    
int n,k,a,b;    
int main()    
{       
    scanf("%d%d%d%d",&n,&k,&a,&b);    
    int ta=(a)/k;    
    int tb=(b)/k;  
    if (a<(b-1)/k||b<(a-1)/k) {    
        printf("NO\n");    
        return 0;    
    }    
    int mark=0;    
    if (ta<tb) mark=1;     
    int t=max(ta,tb);    
    int sizea=k;    
    int sizeb=k;    
    if (t) sizea=max(1,min(sizea,a/t)),sizeb=max(1,min(sizeb,b/t));    
    for (int i=1;i<=t;i++)    
    {        
        if (!mark) {    
            for (int j=1;j<=min(sizea,a);j++) printf("G");    
            a-=sizea;    
            for (int j=1;j<=min(sizeb,b);j++) printf("B");    
            b-=sizeb;   
			while (a-sizea*(t-i)>k&&a-(sizea+1)*(t-i)>=0&&sizea<k)     
              sizea++;    
            while ((b-sizeb*(t-i)>k||a-sizea*(t-i)==0&&sizeb+b-sizeb*(t-i)>k)&&b-(sizeb+1)*(t-i)>=0&&sizeb<k)     
              sizeb++;  
        }    
        else {    
           for (int j=1;j<=min(sizeb,b);j++) printf("B");    
            b-=sizeb;    
            for (int j=1;j<=min(sizea,a);j++) printf("G");    
            a-=sizea;  
			while ((a-sizea*(t-i)>k||b-sizeb*(t-i)==0&&sizea+a-sizea*(t-i)>k)&&a-(sizea+1)*(t-i)>=0&&sizea<k)     
              sizea++;    
            while (b-sizeb*(t-i)>k&&b-(sizeb+1)*(t-i+1)>=0&&sizeb<k)     
              sizeb++;       
        }    
    }    
    if (!mark) {    
        for (int j=1;j<=max(0,a);j++) printf("G");    
        for (int j=1;j<=max(0,b);j++) printf("B");    
    }    
    else     
    {    
        for (int j=1;j<=max(0,b);j++) printf("B");    
        for (int j=1;j<=max(0,a);j++) printf("G");    
    }    
}    



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值