博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第十四届华中科技大学程序设计竞赛--J Various Tree
阅读量:4676 次
发布时间:2019-06-09

本文共 2201 字,大约阅读时间需要 7 分钟。

链接:

来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

It’s universally acknowledged that there’re innumerable trees in the campus of HUST.

And there are many different types of trees in HUST, each of which has a number represent its type. The doctors of biology in HUST find 4 different ways to change the tree’s type x into a new type y:

1.    y=x+1

2.    y=x-1

3.    y=x+f(x)

4.    y=x-f(x)

The function f(x) is defined as the number of 1 in x in binary representation. For example, f(1)=1, f(2)=1, f(3)=2, f(10)=2.

Now the doctors are given a tree of the type A. The doctors want to change its type into B. Because each step will cost a huge amount of money, you need to help them figure out the minimum steps to change the type of the tree into B. 
Remember the type number should always be a natural number (0 included).

输入描述:

One line with two integers A and B
, the init type and the target type.

输出描述:

You need to print a integer representing the minimum steps.
示例1

输入

5 12

输出

3

说明

The minimum steps they should take: 5->7->10->12. Thus the answer is 3. 开始以为广搜会t,然而并不会
/*    data:2018.5.20    author:gsw    link:https://www.nowcoder.com/acm/contest/106#question*/#define ll long long#define IO ios::sync_with_stdio(false);#include
#include
#include
#include
#include
#include
using namespace std;#define maxn 1000005int brainy[maxn];int vis[maxn];int getbrainy(int a){ int ans=0; while(a>0) { if(a&1)ans++; a=a>>1; } return ans;}void init(){ for(int i=1;i
q; q.push(be); vis[be.x]=1; while(!q.empty()) { be=q.front(); q.pop(); if(be.x==b) { cout<
<
=0&&!vis[be.x-brainy[be.x]]) { vis[be.x-brainy[be.x]]=1; ne.x=be.x-brainy[be.x];ne.st=be.st+1; q.push(ne); } if((be.x-1)>=0&&!vis[be.x-1]) { vis[be.x-1]=1; ne.x=be.x-1;ne.st=be.st+1; q.push(ne); } if(!vis[be.x+brainy[be.x]]) { vis[be.x+brainy[be.x]]=1; ne.x=be.x+brainy[be.x];ne.st=be.st+1; q.push(ne); } }}int main(){ int a,b; init(); scanf("%d%d",&a,&b); bfs(a,b);}

 

转载于:https://www.cnblogs.com/fantastic123/p/9063441.html

你可能感兴趣的文章
EntityFramework 7 Left Join Where Select 奇怪问题
查看>>
关于static静态块的使用和static list的使用
查看>>
spring读取配置文件的几种方式
查看>>
从P1到P7——我在淘宝这7年(转)
查看>>
CRM 2011 Distribute Workflow Activity (MSCRM 2011 分派工作流活动)
查看>>
Qt for Android(转)
查看>>
C++大作业之链表实现的高精度加法,减法,和数组实现的高精度乘法。
查看>>
关于招聘的最新信息
查看>>
Python_列表,元组和字典的异同
查看>>
第十六讲:适配器模式
查看>>
java之网络爬虫介绍(非原创)
查看>>
hive-jdbc获取查询日志慢的问题发现与解决
查看>>
真正的语言能用一句代码输出三角形
查看>>
电子时钟,骷髅时钟
查看>>
优化页面加载速度
查看>>
【机器学习详解】SMO算法剖析(转载)
查看>>
windows8.1 装ubuntu16.04双系统 的记录
查看>>
C#图解教程 第十二章 数组
查看>>
linux常用命令2
查看>>
laravel 关联模型
查看>>