• 【21.58%】【codeforces 746D】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

    【题目链接】:http://codeforces.com/contest/746/problem/D

    【题解】

    最后必然是分成a/k 个绿茶块,b/k个黑茶块;
    如果a/k==b/k
    显然可以交替出现;
    如果a/k>b/k
    则先放k个a,然后放一个b;
    这样a-=k,b-=1;
    则a/k会越来越逼近b/k;
    最后交替出现就可以了。
    balabala

    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define rei(x) scanf("%d",&x)
    #define rel(x) scanf("%I64d",&x)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    //const int MAXN = x;
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    
    int n,k,a,b;
    char t[2];
    string ans = "";
    
    int main()
    {
        //freopen("F:\rush.txt","r",stdin);
        rei(n);rei(k);rei(a);rei(b);
        t[0]='G',t[1] = 'B';
        if (a < b)
            swap(a,b),swap(t[0],t[1]);
        bool ok = false;
        while ( (a/k)>(b/k))
        {
            if (a<k || b<1)
                break;
            rep1(i,1,k)
                ans+=t[0];
            ans+=t[1];
            a-=k;
            b--;
        }
        while (a>0 && b>0)
        {
            int ma = min(a,k);
            rep1(i,1,ma)
                ans+=t[0];
            a-=ma;
            ma = min(b,k);
            rep1(i,1,ma)
                ans+=t[1];
            b-=ma;
        }
        if (a>k)
        {
            puts("NO");
            return 0;
        }
        rep1(i,1,a)
            ans+=t[0];
        if (b>k)
        {
            puts("NO");
            return 0;
        }
        rep1(i,1,b)
            ans+=t[1];
        cout << ans << endl;
        return 0;
    }
  • 相关阅读:
    CentOS系统一键部署jdk,maven,tomcat,mysql
    使用sed在源文件上直接替换某一行的内容,只替换第一次找到的那行
    MLPerf 机器学习基准测试实战入门(一)NAVIDA-GNMT
    SpringBoot Controller接收参数的几种常用方式(转)
    使用延时队列DelayQueue
    Oracle、MySql、SQLServer 数据分页查询(转)
    SqlServer收缩日志
    防火墙升级导致产环境服务中止20小时的问题
    NFS相关
    jquery.validate不使用submit提交,而是使用button按钮提交
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626798.html
Copyright © 2020-2023  润新知