結果

問題 No.3408 1215 Segments
コンテスト
ユーザー GOTKAKO
提出日時 2025-12-15 02:02:57
言語 C++17
(gcc 13.3.0 + boost 1.89.0)
結果
WA  
実行時間 -
コード長 3,507 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,189 ms
コンパイル使用メモリ 207,672 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2025-12-15 02:03:23
合計ジャッジ時間 25,440 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 38 WA * 8
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    vector<vector<int>> V = {
        {4,5,6},
        {},
        {0,2,3,4,6},
        {0,2,3,5,6},
        {1,2,3,5},
        {0,1,3,5,6},
        {1,3,4,5,6},
        {0,2,5},
        {},
        {0,1,2,3,5}
    };
    auto f = [&](vector<int> C) -> bool {
        vector<int> ap(7);
        for(int i=0; i<10; i++) for(auto v : V.at(i)) ap.at(v) += C.at(i);
        for(int nine=0; nine<=C.at(9); nine++){
            vector<int> memo = ap;
            ap.at(6) += nine;
            int c = ap.at(6);
        
            int more = c-ap.at(3);
            bool ok = true;
            if(more >= 0 && more <= C.at(0)){
                ap.at(3) += more;
                ap.at(0) += C.at(0)-more,ap.at(1) += C.at(0)-more,ap.at(2) += C.at(0)-more;
                more = c-ap.at(5);
                if(more >= 0 && more <= C.at(1)){
                    ap.at(2) += more,ap.at(5) += more;
                    ap.at(1) += C.at(1)-more,ap.at(4) += C.at(1)-more;
                    more = c-ap.at(0);
                    if(more >= 0 && more <= C.at(6)){
                        ap.at(0) += more;
                        more = c-ap.at(1);
                        if(more >= 0 && more <= C.at(7)){
                            ap.at(1) += more;
                        }
                        else ok = false;
                    }
                    else ok = false;
                }
                else ok = false;
            }
            else ok = false;

            if(ok) for(int i=0; i<6; i++) if(ap.at(i) != ap.at(i+1)){ok = false; break;}
            ap = memo;
            if(ok) return true;
        }
        return false; 
    };

    vector<int> C(10);
    string s; cin >> s;
    int n = s.size(); 
    bool smallest = true;
    string answer(n,'9');
    auto dfs = [&](auto dfs,int digit,int v) -> void {
        if(v >= 10) return;
        for(int i=0; digit<=n+1; i++,digit++){
            C.at(v) = i;
            if((digit == n || (smallest && digit == n+1)) && i && f(C)){
                auto memo = C;
                if(digit == n+1){
                    smallest = false;
                    answer = "";
                    for(int i=1; i<=9; i++) if(C.at(i)) C.at(i)--,answer += '0'+i;
                    for(int i=0; i<=9; i++) while(C.at(i)--) answer += '0'+i;
                    memo = C;
                }
                else{
                    string now = "";
                    auto dfs2 = [&](auto dfs2,int d) -> bool {
                        if(d == n) return true;
                        int c = s.at(d)-'0';
                        if(C.at(c)){
                            C.at(c)--,now += s.at(d);
                            if(dfs2(dfs2,d+1)) return true;
                            C.at(c)++,now.pop_back();
                        }
                        for(int i=c+1; i<=9; i++) if(C.at(i)){
                            C.at(i)--,now += '0'+i;
                            for(int k=0; k<=9; k++) while(C.at(k)--) now += '0'+k;
                            return true;
                        }
                        return false;
                    };
                    if(dfs2(dfs2,0)) answer = min(answer,now),smallest = false;
                    C = memo;
                }            
            }
            dfs(dfs,digit,v+1);
        }
        C.at(v) = 0;
    };
    dfs(dfs,0,0);
    cout << answer << endl;
}
0