結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー nebukuro09nebukuro09
提出日時 2021-07-30 21:16:43
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 115 ms / 2,000 ms
コード長 1,966 bytes
コンパイル時間 704 ms
コンパイル使用メモリ 109,060 KB
実行使用メモリ 42,216 KB
最終ジャッジ日時 2023-09-04 13:31:00
合計ジャッジ時間 3,085 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 114 ms
41,856 KB
testcase_17 AC 115 ms
41,348 KB
testcase_18 AC 110 ms
41,036 KB
testcase_19 AC 109 ms
42,216 KB
testcase_20 AC 80 ms
9,764 KB
testcase_21 AC 81 ms
10,224 KB
testcase_22 AC 81 ms
9,908 KB
testcase_23 AC 8 ms
5,852 KB
testcase_24 AC 94 ms
34,728 KB
testcase_25 AC 41 ms
36,372 KB
testcase_26 AC 109 ms
41,848 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;

void main() {
    auto s = readln.split;
    auto N = s[0].to!int;
    auto M = s[1];
    auto A = 0 ~ readln.split.map!(to!int).array;

    auto ans = new int[](N);

    bool same(int i) {
        return A[i] > 0;
    }

    int larger(int i) {
        foreach (j; i+1..10) if (A[j] > 0) return j;
        return -1;
    }

    void build_ans(int idx) {
        foreach (i; idx..N) {
            foreach (j; 1..10) if (A[j]) {
                ans[i] = j;
                A[j] -= 1;
                break;
            }
        }
    }

    bool dfs(int i) {
        if (i == N) {
            return false;
        }

        int n = (M[i] - '0').to!int;

        if (same(n)) {
            ans[i] = n;
            A[n] -= 1;
        } else if (larger(n) != -1) {
            int m = larger(n);
            ans[i] = m;
            A[m] -= 1;
            build_ans(i+1);
            return true;
        } else {
            return false;
        }

        auto ret = dfs(i+1);
        if (ret) {
            return true;
        } else if (larger(n) != -1) {
            A[n] += 1;
            int m = larger(n);
            ans[i] = m;
            A[m] -= 1;
            build_ans(i+1);
            return true;
        } else {
            A[n] += 1;
            return false;
        }
    }

    if (sum(A) > M.length) {
        build_ans(0);
        ans.map!(to!string).join().writeln;
        return;
    }

    if (sum(A) < M.length) {
        writeln(-1);
        return;
    }

    auto ret = dfs(0);
    if (ret) {
        ans.map!(to!string).join().writeln;
    } else {
        writeln(-1);
    }
}

long powmod(long a, long x, long m) {
    long ret = 1;
    while (x) {
        if (x % 2) ret = ret * a % m;
        a = a * a % m;
        x /= 2;
    }
    return ret;
}
0