Codeforces 746D Green and Black Tea(构造)

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


题意:有n杯茶,分G,B两种,分别有a,b杯。 要求在连续喝一种茶不能超过k次。输出喝茶的顺序


题解:构造一下就好了,代码写的好挫啊 Σ( ° △ °|||)︴


代码如下:


#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e5+10;
char str[maxn];
int main()
{
	int n,k,a,b;
	while(scanf("%d%d%d%d",&n,&k,&a,&b)!=EOF)
	{
		if(a>=b)
		{
			int cnt=a/k;//a被分成几块 
			double num=a*1.0/k;
			if(num==cnt)
				cnt--;
			if(cnt==0)
				cnt=1;
			int count=cnt;
			cnt=b/count;//b每一块最少要填几个 
			if(cnt>k)
				cnt=k;
			if(cnt==0)
				cnt=1; 
			int temp=0;
			while(a>0&&b>0)
			{
				cnt=a/k;
				num=a*1.0/k;
				if(num==cnt)
					cnt--;
				if(cnt==0)
					cnt=1;
				count=cnt;
				cnt=b/count;//每次都要更新cnt 
				if(cnt>k)
					cnt=k;
				if(cnt==0)
					cnt=1; 
				if(a>=k)
				{
					for(int i=0;i<k;++i)
						str[temp++]='G';
					a-=k;
				}
				else 
				{
					for(int i=0;i<a;++i)
						str[temp++]='G';
					a-=a;
				}
				if(a==0)
					cnt=k;
				if(b>=cnt)
				{
					for(int i=0;i<cnt;++i)
						str[temp++]='B';
					b-=cnt;
				}
				else
				{
					for(int i=0;i<b;++i)
						str[temp++]='B';
					b-=b;
				}
			}
			if(a>=k)
			{
				for(int i=0;i<k;++i)
					str[temp++]='G';
				a-=k;
			}
			else 
			{
				for(int i=0;i<a;++i)
					str[temp++]='G';
				a-=a;
			}
			if(a!=0 || b!=0)
				printf("NO\n");
			else{
				str[temp]='\0';
				printf("%s\n",str);
			}
		}
		else
		{
			int cnt=b/k;
			double num=b*1.0/k;
			if(num==cnt)
				cnt--;
			if(cnt==0)
				cnt=1;
			int count=cnt;
			cnt=a/count;
			if(cnt>k)
				cnt=k;
			if(cnt==0)
				cnt=1;
			int temp=0;
			while(a>0&&b>0)
			{
				cnt=b/k;
				num=b*1.0/k;
				if(num==cnt)
					cnt--;
				if(cnt==0)
					cnt=1;
				count=cnt;
				cnt=a/count;
				if(cnt>k)
					cnt=k;
				if(cnt==0)
					cnt=1;
				if(b>=k)
				{
					for(int i=0;i<k;++i)
						str[temp++]='B';
					b-=k;
				}
				else 
				{
					for(int i=0;i<b;++i)
						str[temp++]='B';
					b-=b;
				}
				if(b==0)
					cnt=k;
				if(a>=cnt)
				{
					for(int i=0;i<cnt;++i)
						str[temp++]='G';
					a-=cnt;
				}
				else
				{
					for(int i=0;i<a;++i)
						str[temp++]='G';
					a-=a;
				}
			}
			if(b>=k)
			{
				for(int i=0;i<k;++i)
					str[temp++]='B';
				b-=k;
			}
			else 
			{
				for(int i=0;i<b;++i)
					str[temp++]='B';
				b-=b;
			}
			if(a!=0 || b!=0)
				printf("NO\n");
			else{
				str[temp]='\0';
				printf("%s\n",str);
			}
		}
	}
	return 0;
}

/*
10 4 9 1
10 5 5 5 
10 3 5 5
16 3 8 8
16 2 4 12
15 3 5 10
15 3 4 11
20 2 12 8
*/



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值