• `Algorithm-Solution` `AcWing` 4726. 寻找数字


    link

    Solution

    It is obvious that

    • If A A A has odd decimal-count, answer is 4...47...7 4...4 7...7 4...47...7
    • Otherwise A A A (even decimal-count) if greater then 7...74...4 7...7 4...4 7...74...4, answer is also 4...47...7 4...4 7...7 4...47...7
    • Otherwise the answer has the same decimal-count with a a a.
      + A greedy-approach is wrong that iterate from the highest decimal if A [ i ] ≤ 4 A[i] \leq 4 A[i]4 then choose 4 4 4.
      A example is 4478 4478 4478, you will get the answer 4477 4477 4477 using this algorithm.
      + The correct approach resembles the greedy with a additional check. For example, now you have the answer a b ? . . . ? ab?...? ab?...? ( ? ? ? denotes un-determined) and have F : 4 s , S : 7 s F: 4s, S: 7s F:4s,S:7s unused ( F + S F + S F+S equals the count of ? ? ?), let A n s w e r Answer Answer be a b ab ab.
      ++ If F > 0, let T = 7...74...4 T = 7...7 4...4 T=7...74...4 where contains S : 7 s S: 7s S:7s and ( F − 1 ) : 4 s (F-1): 4s (F1):4s, if a b 4 T > A ab 4 T > A ab4T>A, then let A n s w e r = a b 4 ? . . . ? Answer = ab4 ?...? Answer=ab4?...?
      ++ Otherwise, A n s w e r = a b 7 ? . . . ? Answer = ab7 ?...? Answer=ab7?...?

    Code

    void __Solve( int _test_id){
        ( void)_test_id;
        //--
        long long a;
        cin >> a;
        vector< int> bits;
        for( auto i = a; i > 0; i /= 10){
            bits.push_back( i % 10);
        }
        reverse( bits.begin(), bits.end());
        long long ans = -1;
        if( bits.size() & 1){
            ans = Form( bits.size() / 2 + 1, false);
        }
        else{
            if( a > Form( bits.size() / 2, true)){
                ans = Form( bits.size() / 2 + 1, false);
            }
            else{
                int f = bits.size() / 2, s = bits.size() / 2;
                ans = 0;
                for( auto i : bits){
                    ans *= 10;
                    bool choose_four = false;
                    if( f > 0){
                        long long suf = 0, power = 1;
                        {
                            for(int ii = 1; ii < (s + f); ++ii){
                                power *= 10;
                            }
                            for( int ii = 0; ii < s; ++ii){
                                suf *= 10;
                                suf += 7;
                            }
                            for( int ii = 0; ii < f - 1; ++ii){
                                suf *= 10;
                                suf += 4;
                            }
                        }
                        if( (ans + 4) * power + suf >= a){
                            choose_four = true;
                        }
                    }
                    if( choose_four){
                        assert( f > 0);
                        -- f;
                        ans += 4;
                    }
                    else{
                        assert( s > 0);
                        -- s;
                        ans += 7;
                    }
                }
            }
        }
        assert( ans >= a);
        cout << ans;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
  • 相关阅读:
    上帝视角看Vue源码整体架构+相关源码问答
    进化计算领域exploration和exploitation的区别
    什么原因导致百度百科建立一直审核不通过?
    LED智能家居灯 开关调光 台灯落地灯控制驱动 降压恒流IC AP5191
    Springboot-MyBatisPlue入门
    如何查看网站的https的数字证书
    程序员都是这样关机的
    如何通过快速的指标组合,查看广告的支出回报率
    手把手教你 centos 7 安装RabbitMQ
    常用的Selenium WebdriverAPI
  • 原文地址:https://blog.csdn.net/qq_66485519/article/details/128184652