結果

問題 No.1630 Sorting Integers (Greater than K)
ユーザー H3PO4H3PO4
提出日時 2021-07-30 21:23:26
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
RE  
実行時間 -
コード長 1,435 bytes
コンパイル時間 157 ms
コンパイル使用メモリ 10,772 KB
実行使用メモリ 58,784 KB
最終ジャッジ日時 2023-10-14 03:16:41
合計ジャッジ時間 5,728 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
8,184 KB
testcase_01 AC 17 ms
8,016 KB
testcase_02 AC 16 ms
8,364 KB
testcase_03 AC 16 ms
8,028 KB
testcase_04 AC 16 ms
8,020 KB
testcase_05 AC 16 ms
8,036 KB
testcase_06 AC 16 ms
8,080 KB
testcase_07 AC 16 ms
8,024 KB
testcase_08 RE -
testcase_09 WA -
testcase_10 AC 17 ms
8,184 KB
testcase_11 RE -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 16 ms
8,036 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 368 ms
55,540 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 233 ms
51,756 KB
testcase_23 AC 115 ms
13,452 KB
testcase_24 RE -
testcase_25 RE -
testcase_26 AC 455 ms
58,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N, K = input().split()
N = int(N)
K = list(int(x) for x in K)
C = list(map(int, input().split()))

ans = []
if len(K) < N:
    for i, c in enumerate(C, 1):
        ans.extend([i] * c)

elif len(K) > N:
    print(-1)
    exit()
else:
    assert len(K) == N

    # 不可能
    tmp = []
    for i, c in enumerate(C, 1):
        tmp.extend([i] * c)
    tmp = reversed(tmp)
    for k, i in zip(K, tmp):
        if k < i:
            break
        elif k > i:
            print(-1)
            exit()
    else:
        print(-1)

    flag = False
    for k in K:
        if flag: break
        if C[k - 1]:
            C[k - 1] -= 1
            ans.append(k)
            continue
        for j in range(k + 1, 10):
            if C[j - 1]:
                C[j - 1] -= 1
                ans.append(j)
                for i, c in enumerate(C, 1):
                    ans.extend([i] * c)
                flag = True
                break

    # 同じになってしまった時の処理
    if ans == K:
        m = 0
        for i in range(N - 1, 0, -1):
            C[ans[i] - 1] += 1
            if m > ans[i]:
                break
            m = ans[i]
            ans.pop()
        for j in range(ans[i] + 1, 10):
            if C[j - 1]:
                C[j - 1] -= 1
                ans[i] = j
                for i, c in enumerate(C, 1):
                    ans.extend([i] * c)
                break

print(''.join(map(str, ans)))
0