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.
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 thata + 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
题目大意:有红茶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");
}
}