746D - Green and Black Tea - CodeForces Solution


constructive algorithms greedy math *1500

Please click on ads to support us..

Python Code:

import sys 
sys.setrecursionlimit(1000000000)
import math
import heapq
import time 
from collections import deque
from collections import defaultdict
mod = 10**9+7
def solve(n,k,a,b):
    if k*(min(a,b)+1)<max(a,b):
        return "NO"
    res = ""
    turn = 0
    while max(a,b)-min(a,b)>=k:
        if turn%2==0:
            if max(a,b)==a:
                res += "G"*k 
                a-=k
            else:
                res += "B"*k 
                b-=k
        else:
            if min(a,b)==a:
                res += "G"
                a -=1
            else:
                res += "B"
                b-=1
        turn +=1
   
    if len(res)==0:
        if max(a,b) == a:
            res += "G"
        else:
            res += "B"
    while min(a,b)>0:
        if res[-1]=="G":
            res+= "B"
            b-=1
        else:
            res += "G"
            a-=1
    while len(res)<n:
        if max(a,b)==a:
            res += "G"
        else:
            res += "B"
    return res


    




n,k,a,b = map(int,input().split())
res2 = solve(n,k,a,b)
print(res2)




C++ Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <utility>
#include <complex>
#define ff first
#define ss second
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF=1e9+250;
LL mod=1e9+7;
const int N=100005;
char ans[N];
int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt","r",stdin);
#endif
	int n,m,a,b;
	cin>>n>>m>>a>>b;
	int pos=0;
	int ta=0,tb=0;
	while(a>0||b>0) {
		if(a<b) {
			if(tb<m) {
				ans[pos]='B';
				tb++;
				ta=0;
				b--;
			} else {
				if(a>0&&ta<m) {
					ta++;
					tb=0;
					ans[pos]='G';
					a--;
				} else {
					puts("NO");
					return 0;
				}
			}
 
		} else if(a>b) {
			if(ta<m) {
				ta++;
				tb=0;
				ans[pos]='G';
				a--;
			} else {
				if(b>0&&tb<m) {
					tb++;
					ta=0;
					ans[pos]='B';
					b--;
				} else {
					puts("NO");
					return 0;
				}
			}
		} else {
			if(ta<m) {
				ta++;
				tb=0;
				ans[pos]='G';
				a--;
			} else if(tb<m) {
				tb++;
				ta=0;
				ans[pos]='B';
				b--;
			} else {
				puts("NO");
				return 0;
			}
		}
		pos++;
	}
	puts(ans);
	return 0;
}


Comments

Submit
0 Comments
More Questions

1920. Build Array from Permutation
494. Target Sum
797. All Paths From Source to Target
1547B - Alphabetical Strings
1550A - Find The Array
118B - Present from Lena
27A - Next Test
785. Is Graph Bipartite
90. Subsets II
1560A - Dislike of Threes
36. Valid Sudoku
557. Reverse Words in a String III
566. Reshape the Matrix
167. Two Sum II - Input array is sorted
387. First Unique Character in a String
383. Ransom Note
242. Valid Anagram
141. Linked List Cycle
21. Merge Two Sorted Lists
203. Remove Linked List Elements
733. Flood Fill
206. Reverse Linked List
83. Remove Duplicates from Sorted List
116. Populating Next Right Pointers in Each Node
145. Binary Tree Postorder Traversal
94. Binary Tree Inorder Traversal
101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree