ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

1100. 抓住那头牛(在atcoder犯的错又来一次,梅开二度)

2022-01-29 12:02:34  阅读:160  来源: 互联网

标签:梅开二度 now ch const atcoder int long 1100 include


题目
题意: 给定整数n,目标数m。(0 <= n,m <= 1e5)
操作1: n–或n++
操作2:n * 2
求最少多少次操作。(可以证明一定有解)
思路: bfs。若m < n,只能通过减法得到,直接出结果。
否则bfs搜索。有可能是乘2之后减1得到结果,所以搜索范围不能局限于<=m,应该局限在1e5以内。上次atcoder也犯了类似的错误,还就内个梅开二度。
时间复杂度: O(1e5)
代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<complex>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
// #include<unordered_map>
#include<list>
#include<set>
#include<queue>
#include<stack>
#define OldTomato ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define fir(i,a,b) for(int i=a;i<=b;++i) 
#define mem(a,x) memset(a,x,sizeof(a))
#define p_ priority_queue
// round() 四舍五入 ceil() 向上取整 floor() 向下取整
// lower_bound(a.begin(),a.end(),tmp,greater<ll>()) 第一个小于等于的
// #define int long long //QAQ
using namespace std;
typedef complex<double> CP;
typedef pair<int,int> PII;
typedef long long ll;
// typedef __int128 it;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const ll inf = 1e18;
const int N = 1e5+10;
const int M = 1e6+10;
const int mod = 1e9+7;
const double eps = 1e-6;
inline int lowbit(int x){ return x&(-x);}
template<typename T>void write(T x)
{
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    if(x>9)
    {
        write(x/10);
    }
    putchar(x%10+'0');
}
template<typename T> void read(T &x)
{
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
#define int long long
int n,m,k,T;
bool vis[N];
void solve()
{
   read(n); read(m);
   if(m <= n)
   {
   	 write(n - m);return ;
   }
   queue<PII> q;
   q.push(make_pair(0,n));
   vis[n] = true;
   while(q.size())
   {
   	 PII tmp = q.front(); q.pop();
   	 int now = tmp.second; int cnt = tmp.first;
   	 if(now == m) {write(cnt); return ;}
   	 int t;
   	 t = now - 1; if(t>=0 && t<N && !vis[t]) { vis[t] = 1; q.push(make_pair(cnt+1,t));}
   	 t = now + 1; if(t>=0 && t<N && !vis[t]) { vis[t] = 1; q.push(make_pair(cnt+1,t));}
   	 t = now * 2; if(t>=0 && t<N && !vis[t]) { vis[t] = 1; q.push(make_pair(cnt+1,t));}
   }
}
signed main(void)
{  
   T = 1;
   // OldTomato; cin>>T;
   // read(T);
   while(T--)
   {
   	 solve();
   }
   return 0;
}

标签:梅开二度,now,ch,const,atcoder,int,long,1100,include
来源: https://blog.csdn.net/xianqiuyigedao/article/details/122741290

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有