第十四届华中科技大学程序设计竞赛--J Various Tree

链接:https://www.nowcoder.com/acm/contest/106/J
来源:牛客网

时间限制: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.
输入例子:
5 12
输出例子:
3

-->

示例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<math.h>
#include<stdio.h>
#include<queue>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 1000005

int 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<maxn;i++)
        brainy[i]=getbrainy(i);
    memset(vis,0,sizeof(vis));
}
class Step
{
    public:
        int x,st;
};
void bfs(int a,int b)
{
    Step be,ne;
    be.x=a;be.st=0;
    queue<Step> q;
    q.push(be);
    vis[be.x]=1;
    while(!q.empty())
    {
        be=q.front();
        q.pop();
        if(be.x==b)
        {
            cout<<be.st<<endl;
            return;
        }

        if(!vis[be.x+1])
        {
            vis[be.x+1]=1;
            ne.x=be.x+1;ne.st=be.st+1;
            q.push(ne);
        }
        if((be.x-brainy[be.x])>=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

时间: 2024-11-06 07:19:56

第十四届华中科技大学程序设计竞赛--J Various Tree的相关文章

第十四届华中科技大学程序设计竞赛 J Various Tree【数值型一维BFS/最小步数】

链接:https://www.nowcoder.com/acm/contest/106/J 来源:牛客网 题目描述 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 doc

第十四届华中科技大学程序设计竞赛决赛同步赛

第十四届华中科技大学程序设计竞赛决赛同步赛 A Beauty of Trees 思维,带权并查集 题意: 长度为 n 的序列,没告诉你具体数是多少.只给出 m 个查询,表示区间 [l,r] 的异或和为 k .但是第 i 个查询如果和前面的查询有矛盾,那就是错误的.输出所有的错误查询. tags: 对于一个查询,我们知道 sum[r] ^ sum[l-1] = k . 建成图就是 r -> (l-1) ,但要快速地求出异或值,就要用带权并查集处理.如有 sum[r]^sum[l-1]=k,即 r

第十四届华中科技大学程序设计竞赛 K Walking in the Forest【二分答案/最小化最大值】

链接:https://www.nowcoder.com/acm/contest/106/K 来源:牛客网 题目描述 It's universally acknowledged that there're innumerable trees in the campus of HUST. Now you're going to walk through a large forest. There is a path consisting of N stones winding its way to

第十四届华中科技大学程序设计竞赛决赛同步赛 Beautiful Land

It's universally acknowledged that there're innumerable trees in the campus of HUST.Now HUST got a big land whose capacity is C to plant trees. We have n trees which could be plant in it. Each of the trees makes HUST beautiful which determined by the

2018年湖南省第十四届大学生计算机程序设计竞赛

目录 题目链接 题目 A题 思路 代码 B题 思路 代码 C题 思路 代码 E题 思路 代码 H题 思路 代码 J题 思路 代码 K题 思路 代码 题目链接 传送门 题目 A题 思路 签到. 代码 #include <set> #include <map> #include <deque> #include <queue> #include <stack> #include <cmath> #include <ctime>

第十五届北京师范大学程序设计竞赛现场决赛题解

C. Captcha Cracker题目大意:给一个字符串,识别出0,2,4,6,9以及英文单词并按照出现顺序输出.通过人数/提交人数:60/62题目解法:直接模拟. F. Find Quailty题目大意:给一个凸多边形,求出从不在多边形内一点??出发走不超过??距离且不进入多边形内部所能到的区域面积.通过人数/提交人数:0/3题目解法:圆面积减去圆和凸多边形交的面积是显然不对的. 如果??不在边界上,过??作两条凸包的切线,那么区域被分为两部分,其中一部分如下图所示,只需要计算圆和简单多边形

HDU 6467 简单数学题 【递推公式 &amp;&amp; O(1)优化乘法】(广东工业大学第十四届程序设计竞赛)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6467 简单数学题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 308    Accepted Submission(s): 150 Problem Description 已知 F(n)=∑i=1n(i×∑j=inCij) 求 F(n) m

湖南省第十届大学生计算机程序设计竞赛:酷酷的单词

1505: 酷酷的单词 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 237 Solved: 88 [Submit][Status][Web Board] Description 输入一些仅由小写字母组成的单词.你的任务是统计有多少个单词是"酷"的,即每种字母出现的次数都不同. 比如ada是酷的,因为a出现2次,d出现1次,而1和2不同.再比如,banana也是酷的,因为a出现3次,n出现2次,b出现1次.但是,bbacccd不是酷的,因

湖南大学第十四届ACM程序设计新生杯 Dandan&#39;s lunch

Dandan's lunch Description: As everyone knows, there are now n people participating in the competition. It was finally lunch time after 3 hours of the competition. Everyone brought a triangular bread. When they were going to eat bread, some people fo