1 条题解

  • 0
    @ 2025-7-13 9:25:35

    显然数量不是很多,广搜构造所有好数,然后二分即可。

    #include <bits/stdc++.h>
    using namespace std;
    #pragma GCC optimize("Ofast")
    #pragma GCC optimize("unroll-loops")
    #define int long long
    #define ll long long
    #define lb(x) ((x) & (-x))
    #define double long double
    #define ff first
    #define ss second
    #define mr make_pair
    #define lz(p) t[p].lz
    #define ans(p) t[p].ans
    #define res(p) t[p].res 
    const int mod = 1e9+7 ;
    const int P = 1e9 + 7 ;
    // const int modu[10] = {1000000007,1000000009,1000000021,
    // 1000000033,1000000087,1000000093,1000000097} ;
    void cmin(auto &x, auto y) {
        x = min(x, y) ;
    }
    void cmax(int &x, int y) {
        x = max(x, y) ;
    }
    int read() {
        char ch ;int s = 0 ;int w = 1;
        while((ch = getchar()) >'9' || ch < '0' )if(ch == '-')w = -1 ;
        while(ch >= '0' && ch <= '9')s = (s << 1) + (s << 3) + (ch ^ 48) , ch = getchar() ;
        return s * w ;
    }
    void print(int x) {
        if(x < 0) putchar('-'), x = -x ;                  
        if(x > 9)print(x / 10) ;
        putchar(x % 10 + '0') ;
    }                                   
    void prn(int x) {      
        print(x) ;                                          
        putchar('\n') ;            
    } 
    int pows(int x, int k) {
        long long bace = x, ans = 1 ;
        while(k) {
            if(k & 1) ans *= bace, ans %= mod ;
            bace *= bace, bace %= mod ;
            k >>= 1 ;
        }
        return ans ;
    }
    mt19937 rnd(time(0)^clock()) ;
    int TT ;
    int n,m,k ;
    int a[1000005] ;
    int b[1000005] ;
    vector<int> ans ;
    void work() {
        queue<pair<int,int> > q ;
        for(int i=1;i<=9;i++) q.push({i,-1}) ;
        int tot=0 ;
        while(q.size()) {
            int x=q.front().ff,op=q.front().ss ;
            q.pop() ;
            if(x>1e18) break ;
            ans.push_back(x) ;
            tot++ ;
            // prn(x) ;
            int now=x%10 ;
            if(op==-1) {
                for(int j=0;j<10;j++) {
                    if(j<now) q.push({x*10+j,0}) ;
                    if(j==now) q.push({x*10+j,-1}) ;
                    if(j>now) q.push({x*10+j,1}) ;
                }
            }
            else if(op==0) {
                for(int j=0;j<=now;j++) q.push({x*10+j,0}) ;
            }
            else {
                for(int j=now;j<=9;j++) q.push({x*10+j,1}) ;
            }
        }
        // prn(tot) ;
    }
    signed main(){
        work() ;
        // for(int i=1;i<=10;i++) {
            
        // freopen(("pre_data" + to_string(i) + ".in").c_str(),"r",stdin) ;
        // freopen(("pre_data" + to_string(i) + ".ans").c_str(),"w",stdout) ;
        // freopen("new_data3.in","r",stdin) ;
        // freopen("data2.ans","w",stdout) ;
        // TT=20;
        // init() ;
        TT=1 ;
        TT = read() ;
        while(TT--) {
            int x=read() ;
            int pos=lower_bound(ans.begin(),ans.end(),x)-ans.begin() ;
            prn(pos+1) ;
            
            // work() ;
        }
        cout<<endl;
        // }
        return 0;
    }
    /*
    10
    1
    1000000
    12
    21
    777
    9
    75222
    44
    123456
    654321
    
    */
    
    • 1

    信息

    ID
    14266
    时间
    1000ms
    内存
    1024MiB
    难度
    8
    标签
    递交数
    84
    已通过
    12
    上传者