Technocup 2017 - Elimination Round 1 A题

吐槽

今天真的是蠢哭了,A题错了2个小时,很强势。主要原因在于我认为这道题不用long long存也可以,但是事实并不是这样的,不用long long中途可能会tle,re,我用了深搜和广搜2种方法去写,但是还是写不出了,第五组错了,赛后数据一看,第五组是一个大数据,我发现了问题,改成long long后过了。留个文章提醒自己。。。

DFS版

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
LL S[105], flag, h;

void dfs(LL a, LL b){
    if(a > b || flag) return ;
    if(a == b){
        flag = 1;
        printf("YES\n%I64d\n", h);
        for(int i = 0; i < h; i++){
            if(i) printf(" ");
            printf("%I64d", S[i]);
        }
        printf("\n");
        return ;
    }
    S[h++] = 2*a;
    dfs(2*a, b);
    h--;
    S[h++] = 10*a+1;
    dfs(10*a+1, b);
    h--;
}

int main(){
    LL a, b;
    while(scanf("%I64d%I64d", &a, &b) != EOF){
        flag = h = 0;
        S[h++] = a;
        dfs(a, b);
        if(!flag) printf("NO\n");
    }
    return 0;
}

BFS 版

#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
LL a,b;
vector <LL> vec;

void bfs(){
    priority_queue <LL, vector <LL>, greater<LL> > que;
    vec.clear();
    que.push(a);
    vec.push_back(a);
    bool flag = false;
    while(!que.empty()){
        LL cur = que.top(); que.pop();
        if(2*cur < b) que.push(2*cur), vec.push_back(2*cur);
        else if(2*cur == b){ que.push(2*cur), vec.push_back(2*cur); flag = true; break;}
        if(10*cur+1 < b) que.push(10*cur+1), vec.push_back(10*cur+1);
        else if(10*cur+1 == b){ que.push(10*cur+1), vec.push_back(10*cur+1); flag = true; break;}
    }
    if(flag){
        printf("YES\n");
        int another[1005], cnt = 0;
        int cur = vec.size()-1;
        another[cnt++] = b;
        while(true){
            for(int i = cur-1; i >= 0; i--){
                if(vec[i]*2 == vec[cur] || vec[i]*10 + 1 == vec[cur]){
                    cur = i;
                    another[cnt++] = vec[i];
                    break;
                }
            }
            if(!cur) break;
        }
        printf("%d\n", cnt);
        int first = 1;
        for(int i = cnt-1; i >= 0; i--)
            if(first) printf("%I64d", another[i]), first = 0;
            else printf(" %I64d", another[i]);
        printf("\n");
    }
    else printf("NO\n");
}

int main(){
    while(scanf("%I64d%I64d", &a, &b) != EOF){
        bfs();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值