题解 CF746D 【Green and Black Tea】
# 题目分析
这道题表面上看上去挺简单,其实仔细研究一下还是值得钻研的。我本人做这道题使用的任然是$ DFS01 $背包。不过呢,与往常背包不同的是,这次递归中需要加许多参数。就数据强度来看,栈问题不大。
# 递归过程
我们使用一个栈以及两个临时栈。每次在里面$ push $当前的解。只有“G”与“B”。两个栈分别处理和红茶和和绿茶的情况。两种情况都要考虑到已经连续喝了几次某种茶。
在递归函数里,我们用到$ dep,s,g,h,last $ 这几个变量。分别代表深度、连续喝的总数、绿茶喝的总数、红茶喝的总数以及上次喝的茶是啥。$ 0 $代表绿茶,$1$代表红茶。
### 递归代码:
```cpp
void dfs(int dep,int s,int g,int h,bool last)
{
if(dep>n)
{
stack<string>temp1=temp;
while(!temp1.empty())
{
cout<<temp1.top();
temp1.pop();
}
exit(0);
}
else
{
if(last==0)
{
temp.push("G");
if(g+1<=a)dfs(dep+1,s,g+1,h,!last);
if(s+1<=b&&s<k) dfs(dep+1,s+1,g,h+1,last);
else
{
cout<<"NO"<<endl;
exit(0);
}
}
else
{
temp.push("B");
if(s+1<=b)dfs(dep+1,s,g,h+1,!last);
if(g+1<=a&&s<k) dfs(dep+1,s+1,g,h,last);
else
{
cout<<"NO"<<endl;
exit(0);
}
}
}
if(!temp.empty()) temp.pop();
}
```
# 主函数
最后主函数的程序就简单了。像往常一样,$ dep $总是要是$ 1 $开始,其他都是$ 0 $;但是有一个问题,我们不仅要考虑第一次喝绿茶的情况,第一次还可能是红茶。所以我们在刚开始写递归函数的时候,我们需要递归两遍
### 主函数代码:
```cpp
int main()
{
cin>>n>>k>>a>>b;
dfs(1,0,0,0,0);
dfs(1,0,0,0,1);
return 0;
}
```
# 完整代码
```cpp
#include<bits/stdc++.h>
using namespace std;
int n,k,a,b;
stack<string>temp;
void dfs(int dep,int s,int g,int h,bool last)
{
if(dep>n)
{
stack<string>temp1=temp;
while(!temp1.empty())
{
cout<<temp1.top();
temp1.pop();
}
exit(0);
}
else
{
if(last==0)
{
temp.push("G");
if(g+1<=a)dfs(dep+1,s,g+1,h,!last);
if(s+1<=b&&s<k) dfs(dep+1,s+1,g,h+1,last);
else
{
cout<<"NO"<<endl;
exit(0);
}
}
else
{
temp.push("B");
if(s+1<=b)dfs(dep+1,s,g,h+1,!last);
if(g+1<=a&&s<k) dfs(dep+1,s+1,g,h,last);
else
{
cout<<"NO"<<endl;
exit(0);
}
}
}
if(!temp.empty()) temp.pop();
}
int main()
{
cin>>n>>k>>a>>b;
dfs(1,0,0,0,0);
dfs(1,0,0,0,1);
return 0;
}
```
题解 CF746D 【Green and Black Tea】的更多相关文章
- 【21.58%】【codeforces 746D】Green and Black Tea
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- Codeforces 746D:Green and Black Tea(乱搞)
http://codeforces.com/contest/746/problem/D 题意:有n杯茶,a杯绿茶,b杯红茶,问怎么摆放才可以让不超过k杯茶连续摆放,如果不能就输出NO. 思路:首先,设 ...
- D. Green and Black Tea
先搞多的,搞到相等. (tmd上星期+上上星期简直是弱智,怎么也不会写,总是想着各种a/b,,,踢蹬) #include<bits/stdc++.h> #define lowbit(x) ...
- D. Green and Black Tea 贪心 + 构造
http://codeforces.com/contest/746/problem/D 首先说下一定是NO的情况. 假设a > b 那么,b最多能把a分成b + 1分,如果每份刚好是k的话,那么 ...
- LightOJ1355 Game Of CS(green 博弈)
Jolly and Emily are two bees studying in Computer Science. Unlike other bees they are fond of playin ...
- HIT Winter Day ACM入门
A. Arpa’s hard exam and Mehrdad’s naive cheat 题意:统计1378^n的末尾数字 即统计8^n的末尾数字 n=0时为1 其他情况为{8,4,2,6}中的一个 ...
- L140
一本载有许多时装照片的杂志是用带有光泽的优质纸印制的.A glossy magazine has lots of pictures of fashionable clothes and is prin ...
- Codeforces Round #386 (Div. 2) A+B+C+D!
A. Compote 水题(数据范围小都是水题),按照比例找最小的就行了,3min水过. int main() { int a,b,c; while(~scanf("%d%d%d" ...
- acm博弈论基础总结
acm博弈论基础总结 常见博弈结论 Nim 问题:共有N堆石子,编号1..n,第i堆中有个a[i]个石子. 每一次操作Alice和Bob可以从任意一堆石子中取出任意数量的石子,至少取一颗,至多取出这一 ...
随机推荐
- 深入理解JavaScript垃圾回收
JavaScript中的垃圾回收是自动进行的,在平常开发中我们可能并不在意,但是深入理解JavaScript中的垃圾回收却是必要的; JavaScript 中主要的内存管理概念是 可达性,简而言之就是 ...
- 记一次小米手机安装Google Play(其他手机类似)
记一次小米手机安装Google Play(其他手机类似) 最近换了一款小米10青春版,性价比很高,对于开发者而言,手机自带商店的软件内容往往不能满足需求,而需要单独定制习惯性的APP,博主通过最近的尝 ...
- Github markdown页面内跳转
基本操作: 请看这里 最典型的就是[alt_content](#jump) 但有时, jump是不太好直接看出来的, 比如下面这个标题, 格式复杂, 那如何获取相应的jump呢? 在Github中, ...
- git branch & git remote branch
git branch & git remote branch $ git branch -h usage: git branch [<options>] [-r | -a] [-- ...
- Emoji of github & twitter
Emoji cheat sheet http://www.emoji-cheat-sheet.com/ https://github.com/xgqfrms/emoji-cheat-sheet.com ...
- Fullscreen API All In One
Fullscreen API All In One 全屏显示 https://developer.mozilla.org/en-US/docs/Web/API/Fullscreen_API https ...
- node --experimental-modules & node.js ES Modules
node --experimental-modules & node.js ES Modules how to run esm modules in node.js cli $ node -v ...
- user tracker with ETag
user tracker with ETag 用户追踪, without cookies clear cache bug 实现原理 HTTP cache hidden iframe 1px image ...
- [转]ROS中使用message_filters进行多传感器消息同步
转:http://www.rosclub.cn/post-1030.html 最近实验室老师在做一个多传感器数据采集实验,涉及到了消息同步.所以就学习了ROS官网下的消息同步工具message_fil ...
- Mybatis-06 动态Sql
Mybatis-06 动态Sql 多对一处理 多个学生,对应一个老师 对于学生这边而言,关联多个学生,关联一个老师 [多对一] 对于老师而言,集合,一个老师又很多学生 [一对多] 1.创建数据库 2. ...