Codeforces Round #386(Div. 2)D. Green and Black Tea【思维+构造】
2016-12-19 15:38
330 查看
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
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
Output
Input
Output
Input
Output
题目大意:
构造一个长度为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");
}
}
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
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的字符串,其中一共有两种字符,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 746 D. Green and Black Tea (构造)
- codeforce 386(div2) D. Green and Black Tea
- 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 #386 div2 D. Green and Black Tea
- Codeforces Round #394 (Div. 2)E. Dasha and Puzzle【思维+Dfs构造】
- 【21.58%】【codeforces 746D】Green and Black Tea
- 746 D. Green and Black Tea codeforces
- Codeforces Round #158 (Div. 2)D. Black and White Tree(搜索 + 思维)
- Codeforces Round #311 (Div. 2) B. Pasha and Tea
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle(构造)
- Codeforces Round #311 (Div. 2) B. Pasha and Tea【二分】
- cf 862C Mahmoud and Ehab and the xor 思维+构造
- Codeforces Round #383 (Div. 2)C.Arpa's loud Owf and Mehrdad's evil plan【思维+LCM】
- Codeforces Round #384 (Div. 2) C. Vladik and fractions 构造题
- Codeforces Round #272 (Div. 2) C. Dreamoon and Sums (数学 思维)
- codeforce-231B--- Magic, Wizardry and Wonders (思维+构造)
- Codeforces #324 Div2 B.Kolya and Tanya(dp、思维)
- Codeforces Round #430 (Div. 2) Ilya And The Tree 树上因子 思维 + dfs