zoukankan      html  css  js  c++  java
  • 问题 D: Lunlun Number(bfs+思维)

    问题 D: Lunlun Number
    时间限制: 1 Sec 内存限制: 128 MB

    题目描述
    A positive integer X is said to be a lunlun number if and only if the following condition is satisfied:
    In the base ten representation of X (without leading zeros), for every pair of two adjacent digits, the absolute difference of those digits is at most 1.
    For example, 1234, 1, and 334 are lunlun numbers, while none of 31415, 119, or 13579 is.
    You are given a positive integer K. Find the K-th smallest lunlun number.
    Constraints
    ·1≤K≤105
    ·All values in input are integers.

    输入
    Input is given from Standard Input in the following format:

    K

    输出
    Print the answer.
    样例输入 Copy
    【样例1】
    15
    【样例2】
    1
    【样例3】
    13
    【样例4】
    100000
    样例输出 Copy
    【样例1】
    23
    【样例2】
    1
    【样例3】
    21
    【样例4】
    3234566667
    提示
    样例1解释
    We will list the 15 smallest lunlun numbers in ascending order:
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 21, 22, 23.
    Thus, the answer is 23.
    样例4解释
    Note that the answer may not fit into the 32-bit signed integer type.

    思路:

    其实是个找规律的题,但是写着不是很好写。
    我们可以预处理出来1e5个特殊数,这样就可以(O(1))回答询问了。
    考虑如果预处理:对于每个数来说,都可以(*10)后扩展为别的数,比如(12),可以变成(122,121,123)。所以用(bfs)来打表比较方便。
    首先把(1~9放入队列,每次取出元素,扩展下一层放入队列,要注意边界问题和放入队列的顺序。)

    代码:

    #include <iostream>
    #include <algorithm>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include<map>
    #include<set>
    #include<vector>
    #include<queue>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<ll, ll>PLL;
    typedef pair<int, int>PII;
    typedef pair<double, double>PDD;
    #define I_int ll
    inline ll read()
    {
        ll x = 0, f = 1;
        char ch = getchar();
        while(ch < '0' || ch > '9')
        {
            if(ch == '-')f = -1;
            ch = getchar();
        }
        while(ch >= '0' && ch <= '9')
        {
            x = x * 10 + ch - '0';
            ch = getchar();
        }
        return x * f;
    }
    #define read read()
    #define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
    #define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
    #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    #define repp(i,a,b) for(int i=(a);i<(b);i++)
    #define per(i,a,b) for(int i=(a);i>=(b);i--)
    #define perr(i,a,b) for(int i=(a);i>(b);i--)
    ll ksm(ll a, ll b, ll p)
    {
        ll res = 1;
        while(b)
        {
            if(b & 1)res = res * a % p;
            a = a * a % p;
            b >>= 1;
        }
        return res;
    }
    const int inf = 0x3f3f3f3f;
    #define PI acos(-1)
    const int maxn=1e5+10;
    queue<ll>q;
    vector<ll>ans;
    void init(){
        rep(i,1,9) q.push(i);
        while(!q.empty()){
            ll t=q.front();q.pop();
            ans.push_back(t);
            ///cout<<t<<"**********
    ";
            if(ans.size()>maxn) return ;
            ll cnt=t%10;
             
             
            if(cnt!=0) q.push(t*10*1ll+cnt-1);
            q.push(t*10*1ll+cnt);
            if(cnt!=9) q.push(t*10*1ll+cnt+1);
        }
         
    }
     
    int main(){
        init();
        int k=read-1;
        cout<<ans[k]<<endl;
        return 0;
    }
     
     
     
     
     
     
     
    
    
  • 相关阅读:
    SOCKET缓存
    异步任务调度
    缓存字典
    TBytes缓存多包数据
    通用压缩单元
    hazelcast-jet docker 运行试用
    hazelcast-jet 开源分布式流以及批处理框架
    Easy Python Decompiler 一个很不错的python pyc 反编译工具
    HAProxy Process Management
    pgx zombodb 团队开源的基于rust 开发pg扩展
  • 原文地址:https://www.cnblogs.com/OvOq/p/14877625.html
Copyright © 2011-2022 走看看