That’s One Hanoi-ed Teacher

链接

题意:

  询问一个汉诺塔的状态是否是最优的状态,如果是,询问还有多少步到最终状态。

分析:

  考虑汉诺塔是怎么操作的,首先是考虑F(i)是有i个盘子,从一根柱子完全移到另一根柱子的花费。如果存在x个盘子,那么答案是F(x - 1)+1+F(x-1),为前x-1盘子先从1移动到2,然后第x个盘子移动到3,然后x-1个盘子从2移到3。

  所以对于一个状态,可以先找到最大的盘子x,如果在中间的话,无解。否则使用F(x-1)+1次操作,将它移动到应该在的位置,然后当前的状态又是一样的。

代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} LL ans = ;
bool dfs(int now,vector<int>& A, vector<int> &B, vector<int> &C) {
if (!now) return ;
if (A.size() && A[] == now) {
ans += 1ll << (now - );
A.erase(A.begin());
return dfs(now - , A, C, B);
}
if (C.size() && C[] == now) {
C.erase(C.begin());
return dfs(now - , B, A, C);
}
return ;
}
vector<int> A, B, C; int main() {
int a = read();
for (int i = ; i <= a; ++i) A.push_back(read());
int b = read();
for (int i = ; i <= b; ++i) B.push_back(read());
int c = read();
for (int i = ; i <= c; ++i) C.push_back(read());
if (!dfs(a + b + c, A, B, C)) puts("No");
else printf("%I64d\n", ans);
return ;
}

CF GYM 101196 G That’s One Hanoi-ed Teacher的更多相关文章

  1. CF Gym 102028G Shortest Paths on Random Forests

    CF Gym 102028G Shortest Paths on Random Forests 抄题解×1 蒯板子真jir舒服. 构造生成函数,\(F(n)\)表示\(n\)个点的森林数量(本题都用E ...

  2. Codeforces Gym 100513G G. FacePalm Accounting

    G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513 ...

  3. Codeforces Gym 100637G G. #TheDress 暴力

    G. #TheDress Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100637/problem/G ...

  4. Codeforces Gym 100513G G. FacePalm Accounting 暴力

    G. FacePalm Accounting Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513 ...

  5. CF gym 101933 K King's Colors —— 二项式反演

    题目:http://codeforces.com/gym/101933/problem/K 其实每个点的颜色只要和父亲不一样即可: 所以至多 i 种颜色就是 \( i * (i-1)^{n-1} \) ...

  6. Gym - 101147G G - The Galactic Olympics —— 组合数学 - 第二类斯特林数

    题目链接:http://codeforces.com/gym/101147/problem/G G. The Galactic Olympics time limit per test 2.0 s m ...

  7. Gym 100952 G. The jar of divisors

    http://codeforces.com/gym/100952/problem/G G. The jar of divisors time limit per test 2 seconds memo ...

  8. Codeforces Gym 100203G G - Good elements 标记暴力

    G - Good elementsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...

  9. Codeforces Gym 100203G G - Good elements 暴力

    G - Good elementsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/ ...

随机推荐

  1. [EXCEL] 不能清除剪贴板: We couldn't free up space on the clipboard. Another program might be using it right now

    Excel复制粘贴时出现以下错误,原因是有程序占用了剪切板. We couldn't free up space on the clipboard. Another program might be ...

  2. vs2017 调试时出现 cannot connect to runtime process错误

    用Visual Studio 2017 .net core进行开发时 ,调试运行项目时出现如下错误 解决方案,调试>选项,取消勾选,关闭对JavaScript的调试

  3. Visual Studio 下nuget命令的使用

    从Visual Studio 2012版本开始默认集成了Nuget扩展,在Visual Studio 2010或以下的版本需要单独安装,安装方法如下: 1. “工具”→“扩展和更新...”,弹出扩展管 ...

  4. 常用CSS技术收藏

    常用CSS技术收藏 必须要掌握的技术 盒子模型 定位模型 定位模型 css sprite(雪碧/css精灵)相关 css sprite 坐标定位为何为负以及定位方法 布局 圣杯布局小结 规范 BEM ...

  5. iOS8 CIGlassDistortion滤镜的使用

    iOS8 CIGlassDistortion滤镜的使用 此为CoreImage滤镜的使用 素材 效果 混合用图片 源码: // // ViewController.m // CIGlass // // ...

  6. Linux 系统常见命令功能大全_【all】

    Linux常见快捷键(6个) ctrl + u:剪贴光标前面 ctrl + k:剪贴光标后面 ctrl + y:粘贴 ctrl + r:查找命令 ctrl + insert:复制 shift+ ins ...

  7. SpringBoot整合Redis初实践

    Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理. 有时,为了提升整个网站的性能,在开发时会将经常访问的数据进行缓存,这样在调用这个数据接口时,可以提 ...

  8. ZT 二分插入排序也称折半插入排序

    二分插入排序也称折半插入排序,基本思想是:设数列[0....n]分为两部分一部分是[0...i]为有序序列,另一部分是[i+1.....n]为无序序列,从无序序列中取一个数 x ,利用二分查找算法找到 ...

  9. 10条Linux 命令了解服务器当前性能

    参考:http://www.infoq.com/cn/news/2015/12/linux-performance 1. uptime 如果电脑运行缓慢,执行 uptime 可以大致查看Linux服务 ...

  10. [转]HBase高可用性的新阶段

    From:http://m.csdn.net/article_pt.html?arcid=2823943 Apache HBase是一个面向线上服务的数据库,其原生支持Hadoop的特性,使其成为那些 ...