Codeforces Round #386 (Div. 2) D. Green and Black Tea 数论+贪心
2017-01-11 01:24
232 查看
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 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
output
input
output
input
output
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
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 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
相关文章推荐
- 2.输入系统驱动(2)-input系统框架层分析
- OSI模型与TCP/IP模型
- 2.输入系统驱动(1)-input应用程序设计
- java内存溢出示例(堆溢出、栈溢出)
- wxPython开发总结---将Python源代码打包成exe可执行文件
- shader渲染管线
- ionicCSS04_卡片
- 在云服务器上部署项目(上)
- 基于FFmpeg 和 OpenGL 的音视频合成+过渡特效【C++】
- tomcat 7内存调大也起不来
- 分布式架构的演变
- JavaFX如何实现模态窗口
- 统一的初始化方式
- Mysql导入数据库的方法
- git快速上手教程
- Spring 框架的搭建及测试
- Codeforces Round #386 (Div. 2) C. Tram 数学、讨论
- 反射注解浅引
- php7-nginx-composer 安装laravel
- Shiro-从数据表中初始化资源和权限