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杯茶,分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 110 5 5 5 10 3 5 516 3 8 816 2 4 1215 3 5 1015 3 4 1120 2 12 8*/