Codeforces 746D Green and Black tea (construction) __ Construction

Source: Internet
Author: User

Green and black tea time limit/test 1 second memory limit per test 256 megabytes input standard input output standard Output

Innokentiy likes tea is very much more and today he wants to drink exactly n cups. He would is happy to drink the but he had exactly n tea bags, a of them are green and B are black.

Innokentiy doesn ' t like to drink's same tea (green or black) is more than k times in a row. Your task is to determine "order of brewing tea bags so" innokentiy'll be able to drink n cups of tea, without Dr Inking 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 "I" 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 T Ea. It is guaranteed that a + b = N. Output

If it is impossible to drink n cups the 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 to be green. If some character equals ' B ', then the corresponding cup of tea should is 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


The point: There are n cups of tea, divided into two kinds of g,b, there are a,b cups respectively. It is not required to drink a tea in a row for more than k times. Output the order of tea


The puzzle: The construction is good, the code writes the good setback Ah Σ (° °| | |) ︴


The code is as follows:


#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 is divided into several double num=a*1.
			0/k;
			if (num==cnt) cnt--;
			if (cnt==0) cnt=1;
			int count=cnt;
			cnt=b/count;//b each block must fill in at least several 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;//each time to update the 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]= ';
			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]= ';
			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 * *



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.