Codeforces Round #386 (Div. 2) D. Green and Black Tea 数论+贪心

129 篇文章 0 订阅
58 篇文章 0 订阅

D. 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 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 nka and b (1 ≤ k ≤ n ≤ 1050 ≤ 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 thata + 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


Source

Codeforces Round #386 (Div. 2)


My Solution

题意:喝掉n袋茶,其中a袋绿茶b袋红茶,连续喝相同的茶最多k次,如果可以全喝完则输出喝茶的序列,如果不能则输出NO


数论+贪心

char a为个数多的那个茶的字母的代表,同理b为少的那个字母,aa为a的个数,bb为b的个数

首先int realk = aa / (bb + 1); if(realk * (bb + 1) < aa) realk++; 如果 realk > k 则 ans 为 NO;

否则把aa 分成 bb + 1组,int ok = aa % (bb + 1);if(ok == 0){ok = bb + 1;}

从而有ok组是realk个a,剩余的是realk-1个a,每组间用b隔开。

复杂度 O(n)


#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
typedef long long LL;
const int maxn = 1e6 + 8;

string ans;

int main()
{
    #ifdef LOCAL
    freopen("d.txt", "r", stdin);
    //freopen("d.out", "w", stdout);
    int T = 6;
    while(T--){
    #endif // LOCAL
    ios::sync_with_stdio(false); cin.tie(0);

    LL n, k, a, b;
    cin >> n >> k >> a >> b;
    int aa = max(a, b), bb = min(a, b);
    char x, y;
    if(aa == a){
        x = 'G';
        y = 'B';
    }
    else{
        x = 'B';
        y = 'G';
    }

    int realk = aa / (bb + 1);
    if(realk * (bb + 1) < aa) realk++;

    if(realk <= k){

        //cout << realk << endl;
        int ok = aa % (bb + 1);
        if(ok == 0){
            ok = bb + 1;
        }
        int cnt = 0, oki = 0;
        ans.clear();
        for(int i = 0; i < n; i++){
            if(cnt == realk){
                ans += y;
                cnt = 0;
                oki++;
                if(oki == ok){
                    realk--;
                }
            }
            else{
                cnt++;
                ans += x;
            }
        }
        cout << ans << endl;
    }
    else cout << "NO" << endl;


    #ifdef LOCAL
    cout << endl;
    }
    #endif // LOCAL
    return 0;
}



  Thank you!

                                                                                                                                               ------from ProLights

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值