先说D题,思路就是如果a大于b就让a/2,如果a是奇数就先加一,在加上b-a的值就是答案,代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long a,b,ans=0;
    cin>>a>>b;
    if(b>=a)
    {
        ans=b-a;
    }
    else
    {
        while(1)
        {
            if(a%2==0)
            a=a/2;
            else
            {
                a=a+1;
                a=a/2;
                ans++;
            }
            ans++;
            if(b>=a)
            {
                ans+=b-a;
                break;
            }
        }
    }
    cout<<ans;
}

C题当时思考的就是找从1到n的最短路,但是用的是深搜,所以不是超时就是错误,赛后又想了。发现就是一道很简单的广搜,代码:

#include<bits/stdc++.h>
using namespace std;
int vis[100005];
vector<int> p[100005];
struct node{
    int x,y;
};
int ans = 1e9;
void bfs(int x){
    queue<node> q;
    q.push(node{x,0});
    while(!q.empty()){
        node now = q.front();
        q.pop();
        if(now.x == 1){
            ans = now.y;
            break ;
        }
        int siz = p[now.x].size();
        for(int i=0;i<siz;i++){
            if(vis[p[now.x][i]] == 0){
                vis[p[now.x][i]] = 1;
                q.push(node{p[now.x][i] , now.y + 1});
            }
        }
    }
}
int main()
{
    int n,m;
    cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        int c,d;
        cin>>c>>d;
        p[c].push_back(d);
        p[d].push_back(c);
    }
    vis[n] = 1;
    bfs(n);
    cout<<ans - 1<<endl;
}

 

posted on 2020-05-06 21:54  小灰灰的父亲  阅读(211)  评论(0)    收藏  举报