Codeforces Round #386(Div. 2)D. Green and Black Tea【思维+构造】
来源:互联网 发布:淘宝详情页英文素材 编辑:程序博客网 时间:2024/06/01 19:39
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 exactlyn tea bags, a of them are green andb 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 drinkn cups of tea, without drinking the same tea more thank times in a row, or to inform that it is impossible. Each tea bag has to be used exactly once.
The first line contains four integers n,k, a andb (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 thata + b = n.
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.
5 1 3 2
GBGBG
7 2 2 5
BBGBGBB
4 3 4 0
NO
题目大意:
构造一个长度为N的字符串,其中一共有两种字符,B/G.一共有a个G,b个B,需要保证答案字符串没有超过K个连续的相同字符。
思路:
我们将数量较多的字符每个部分都用K个相同的部分来组成,然后我们将数量较少的字符往里边插。
Ac代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int n,k,a,b;char ans[10000600];int main(){ while(~scanf("%d%d%d%d",&n,&k,&a,&b)) { char aa='G'; char bb='B'; if(a>b) { swap(a,b); swap(aa,bb); } for(int i=0;i<n;i++) { ans[i]=bb; } int flag=0; for(int i=k;i<n;i+=k+1) { if(a>0) { ans[i]=aa; a--; } else { flag=1; } } if(flag==0) { for(int i=0;i<n;i++) { if(a==0)break; if(ans[i]==aa||(i>0&&ans[i-1]==aa)||(i+1<n&&ans[i+1]==aa))continue; ans[i]=aa; a--; } for(int i=0;i<n;i++) { printf("%c",ans[i]); } printf("\n"); } else printf("NO\n"); }}
- Codeforces Round #386(Div. 2)D. Green and Black Tea【思维+构造】
- Codeforces Round #386 (Div. 2)D. Green and Black Tea
- Codeforces Round #386 (Div. 2) D. Green and Black Tea
- Codeforces Round #386 (Div. 2) D. Green and Black Tea 数论+贪心
- Codeforces Round #386 (Div. 2) 746D Green and Black Tea
- 【Codeforces 746D Green and Black Tea】+ 构造
- Codeforces 746D Green and Black Tea(构造)
- codeforces 746 D. Green and Black Tea (构造)
- Codeforces 746D Green And Black Tea 构造
- CodeForces 746D Green and Black Tea
- 746 D. Green and Black Tea codeforces
- codeforces #386 div2 D. Green and Black Tea
- 【21.58%】【codeforces 746D】Green and Black Tea
- codeforce 386(div2) D. Green and Black Tea
- codefroces 746D Green and Black Tea
- 746D Green and Black Tea
- Codeforces Round #158 (Div. 2)D. Black and White Tree(搜索 + 思维)
- Codeforces Round #410 (Div. 2) D. Mike and distribution 思维
- ora-00907缺失右括号
- 记录sublimeText3PacketControl的安装过程
- ACE框架使用心得2—日期选择控件
- ssh_exchange_identification: Connection closed by remote host
- spring-boot 完美解决:could not initialize proxy - no Session
- Codeforces Round #386(Div. 2)D. Green and Black Tea【思维+构造】
- android笔记,点击notification时的跳到栈顶
- SpringMVC注解配置
- 29-fork 函数与文件共享
- <<C 陷阱与缺陷>>读书笔记
- ZCMU----1776
- 有关单表处理数据重复问题具体sql
- JQuery--实现标签页
- calico跨主机node访问pod问题