結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー nebukuro09nebukuro09
提出日時 2021-07-30 21:12:08
言語 D
(dmd 2.105.2)
結果
WA  
実行時間 -
コード長 1,776 bytes
コンパイル時間 991 ms
コンパイル使用メモリ 110,192 KB
実行使用メモリ 42,448 KB
最終ジャッジ日時 2023-09-04 13:30:53
合計ジャッジ時間 4,939 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 1 ms
4,384 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,384 KB
testcase_16 AC 112 ms
42,184 KB
testcase_17 AC 112 ms
42,448 KB
testcase_18 AC 106 ms
41,344 KB
testcase_19 AC 106 ms
40,792 KB
testcase_20 AC 80 ms
10,172 KB
testcase_21 AC 79 ms
10,432 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 90 ms
34,624 KB
testcase_25 AC 36 ms
37,044 KB
testcase_26 AC 106 ms
41,992 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;
        }
    }

    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