首页 > 代码库 > CodeForces 507C Guess Your Way Out!(二叉树)
CodeForces 507C Guess Your Way Out!(二叉树)
实在太困 题都看错
先贴王大神的代码有空再做遍
#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>#include <iostream>#include <cstring>#include <queue>using namespace std;typedef __int64 LL;LL h,n;LL now1,now2;LL ans;void dfs(LL q,LL w,int vis,int high,LL sum){ if(high == h) { return; } LL mid = (q + w)/2; if(vis == 0) { if(n <= mid) { ans += (sum * 2 - 2)/2 + 1; dfs(q,mid,0,high + 1,sum/2); } else { ans++; dfs(mid+1,w,1,high + 1,sum/2); } } else { if(n <= mid) { ans++; dfs(q,mid,0,high + 1,sum/2); } else { ans += (sum * 2 - 2)/2 + 1; dfs(mid+1,w,1,high + 1,sum/2); } }}int main(){ int i; while(scanf("%I64d %I64d",&h,&n)!=EOF) { ans = 0; now1 = 1; now2 = 1; for(i = 0; i < h; i++) { now2 *= 2; } LL m = (now1 + now2)/2; if(n <= m) { ans += 1; dfs(now1,m,0,1,now2/2); } else { ans += (((now2 * 2) - 2)/2) + 1; dfs(m+1,now2,1,1,now2/2); } printf("%I64d\n",ans); } return 0;}
CodeForces 507C Guess Your Way Out!(二叉树)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。