zoukankan      html  css  js  c++  java
  • Codeforces Round #386 (Div. 2) D. Green and Black Tea

    D. Green and Black Tea
    time limit per test1 second
    memory limit per test256 megabytes
    inputstandard input
    outputstandard 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杯茶,a杯绿茶,b杯红茶,有个人要求每隔k杯相同的茶就换另一种茶喝,构造一个喝茶顺序的序列,如果不可能实现就输出NO。多解题

    题解:首先判断不可能的条件。
    首先如果任意一种茶为0杯,并且换茶杯数小于单种茶的总数时,说明不可能构造出每隔k杯相同就更换茶的序列,因为可以作为分割的茶为0杯。
    第二种不可能的情况。举一个例子,有十本书,用两个板子隔开,可以分成3份,因为10%3!=0,所以份中最大的一份是10/3+1=4,意思是平均分成3份,每份3本,余下1本,那么将那1本随机放到其中一份上,最多的那份就有4本书。再比如,如果是11本书用2个板子隔成3份,就是11/3=3(每份3平均3本书),余下2本,随机取两份放入,也就是说有两份是最多的,达到了4本书。可以得知,做了除法操作后,余下的书的数量是小于分割的份数的,将余下的书又发配给每一份,这样最多书的份数就有 原来平均分后+1。(中国剩余定理)
    回到题目中,将数量较多的茶类比为书,数量较少的茶作为隔板,就可以分出 较少茶数+1份 的较多的茶,这是在平均分的情况下,那么取出每份最多时有多少杯茶,即 max/(min+1)+1,如果每份有的茶的数量,大于k,说明用少的茶作为分割,根本不可能做到每隔k杯相同的茶就换另一种茶。k小于每份的最小限度了。判定为NO。
    首先算出平均分成min+1份后,max的茶还余下几杯,如果不余,则max/(min+1)小于等于k时是有解的,如果有余,则每份最多有[max/(min+1)]+1杯相同的茶,因此必须完全小于k时,才有解。
    输出部分只需要按照谁多就输出谁的原则,拿较少的来分割即可,若分到了两种茶剩余一样多的时候,则交替输出两种茶。

    #include<stdio.h>
    #include<algorithm>
    using namespace std;
    int main()
    {
        int n,k,a,b;
        while(scanf("%d%d%d%d",&n,&k,&a,&b)!=EOF)
        {
            if((a==0||b==0)&&n>k)
            {
                printf("NO
    ");
                continue;
            }
            int judge=max(a,b)%(min(a,b)+1);
            if(judge==0&&max(a,b)/(min(a,b)+1)>k)
            {
                printf("NO
    ");
                continue;
            }
            if(judge!=0&&max(a,b)/(min(a,b)+1)>=k)
            {
                printf("NO
    ");
                continue;
            }
            int flag=0,num=0,ei=0;
            while(num!=n)
            {
                if(a>b)
                {
                    if(ei==1)flag++;
                    while(flag!=k&&num!=n)
                    {
                        printf("G");
                        ei=1;
                        a--;
                        flag++;
                        num++;
                        if(a==0)break;
                    }
                    flag=0;
                    if(num!=n&&b!=0)
                    {
                        printf("B");
                        ei=-1;
                        b--;
                        num++;
                    }
                }
                else if(a<b)
                {
                    if(ei==-1)
                    {
                        flag++;
                    }
                    while(flag!=k&&num!=n)
                    {
                        printf("B");
                        ei=-1;
                        b--;
                        num++;
                        flag++;
                        if(b==0)break;
                    }
                    flag=0;
                    if(num!=n&&a!=0)
                    {
                        printf("G");
                        ei=1;
                        a--;
                        num++;
                    }
                }
                else if(a==b)
                {
                    if(ei==0)ei=-1;
                    break;
                }
            }
            while(num!=n)
            {
                if(ei==1)
                {
                    printf("B");
                    b--;
                    num++;
                    ei=-1;
                }
                else if(ei==-1)
                {
                    printf("G");
                    a--;
                    num++;
                    ei=1;
                }
            }
            printf("
    ");
        }
    }
    
  • 相关阅读:
    POJ 2255. Tree Recovery
    Ural 1011. Conductors
    Ural 1010. Discrete Function
    算法导论学习 之 解递归式
    算法导论学习 之 渐进符号
    kubernetes-集群构建
    kubernetes-集群备份和恢复
    kubernetes-概念
    Kubernetes-常用命令
    kubernetes-单机实验(入门)
  • 原文地址:https://www.cnblogs.com/kuronekonano/p/11794332.html
Copyright © 2011-2022 走看看