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

Source: Internet
Author: User
Tags pack

D. Green and black tea time limit/test 1 second memory limit per test 256 megabytes input standard input output Standa RD 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 ntea, a of bags them green and B are.

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 thata + 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 topic general idea: have black tea a pack, Black Cup B pack, a+b=n, construct a scheme to make each kind of tea continuous quantity does not exceed K, if not exist legal scheme to output no

Solving: Constructing

This question should first be divided into the number of sections that divide the two teas. T=max (a/k,b/k)

Then the number of a,b in each paragraph is calculated according to the number of segments, the note cannot exceed K, nor be less than 1.

However, if the T segment is constructed directly, the remaining a,b may exceed K, so in the course of construction we will change the value of A,b in each segment according to the remainder of the calculation.

#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using NA    
Mespace 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&LT;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) Prin    
            TF ("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");     }    
}



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.