题解 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可以从任意一堆石子中取出任意数量的石子,至少取一颗,至多取出这一 ...
随机推荐
- ysoserial Commons Collections1反序列化研究
Apache Commons Collections1反序列化研究 环境准备 Apache Commons Collections 3.1版本 IDEA 需要一些java基础,反射.类对象.Class ...
- js 可选链 & 空值合并 In Action
js 可选链 & 空值合并 In Action const obj = { props: { name: 'eric', }, // prop, 不存在的属性 ️ }; console.log ...
- HHVM的全称是"HipHop for PHP",开放源代码。采用PHP许可证授权!
http://hhvm.com/ https://github.com/xgqfrms/hhvm 什么是HHVM高性能服务器? HHVM是由Facebook公司出品的高性能开源服务器,用来执行hack ...
- HTML a Tag All In One
HTML a Tag All In One HTML <a> target https://developer.mozilla.org/en-US/docs/Web/HTML/Elemen ...
- React.memo All In One
React.memo All In One https://reactjs.org/docs/react-api.html#components React.memo const MyComponen ...
- npm-run-all
npm-run-all npm scripts https://www.npmjs.com/package/npm-run-all A CLI tool to run multiple npm-scr ...
- CSS & SASS & SCSS & less
CSS & SASS & SCSS & less less vs scss https://github.com/vecerek/less2sass/wiki/Less-vs. ...
- Web Share API
Web Share API https://w3c.github.io/web-share/ Web Share API, W3C Editor's Draft 15 April 2020 https ...
- Google can't be accessed again, today is shit day
Google can't be accessed again, today is shit day 2019.11.28 12:00~20:56 holy shit (pile of poop) Go ...
- [Android 搞机]Twrp 中清除 data 和搞机清除的区别
近日搞机,用上了 Havoc OS.突然发现设置里有个"加密手机",于是手贱点了进去.手机立刻重启了,然后卡在开机第一屏.遂进 Twrp 高级清除中清除了 data 并重新刷入.重 ...