@xunuo
2017-01-18T21:22:44.000000Z
字数 2033
阅读 1007
time limit per test1 second memory limit per test256 megabytes
来源::codeforces 746 D Green and Black Tea
模拟
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.
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.
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.
5 1 3 2
GBGBG
7 2 2 5
BBGBGBB
4 3 4 0
NO
题意
有两种字符:'B'和'G',
输入表示总共有n个字符,最多只能有k个连续,a表示字符'G'的个数,b表示字符'B'的个数
解法:定义一个字符型数组s[],以7 2 2 5为列
先判断哪种字符较多:这里B字符较多,先将s数组全部初始化为B,然后再根据K的多少来插入G;
完整代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
char s[1000000];
int main()
{
int n,k,a,b,flag,flag1;
while(scanf("%d%d%d%d",&n,&k,&a,&b)!=EOF)
{
memset(s,'\0',sizeof(s));
flag1=0;
flag=1;
if(a>b)///'G'字符较多;
{
for(int i=0;i<n;i++)
s[i]='G';///s初始化全为'G';
for(int i=k;i<n;i+=k+1)///在s中插入'B';
{
if(b-flag1>0)
{
s[i]='B';
flag1++;
}
else
flag=0;
}
if(flag==1)
{
for(int i=0;i<n;i++)
{
if(b==flag1)
break;
if(s[i]=='B'||(i>0&&s[i-1]=='B')||(i<n-1&&s[i+1]=='B'))
continue;
s[i]='B';
flag1++;
}
}
}
else
{
for(int i=0;i<n;i++)
s[i]='B';
for(int i=k;i<n;i+=k+1)
{
if(a-flag1>0)
{
s[i]='G';
flag1++;
}
else
flag=0;
}
if(flag==1)
{
for(int i=0;i<n;i++)
{
if(a==flag1)
break;
if(s[i]=='G'||(i>0&&s[i-1]=='G')||(i<n-1&&s[i+1]=='G'))
continue;
s[i]='G';
flag1++;
}
}
}
if(flag==0)
printf("NO");
else
printf("%s",s);
printf("\n");
}
return 0;
}