結果

問題 No.205 マージして辞書順最小
ユーザー nightyhotdognightyhotdog
提出日時 2018-05-25 02:04:14
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 53 ms / 5,000 ms
コード長 1,545 bytes
コンパイル時間 401 ms
コンパイル使用メモリ 10,964 KB
実行使用メモリ 8,696 KB
最終ジャッジ日時 2023-09-11 03:18:25
合計ジャッジ時間 2,088 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 20 ms
8,552 KB
testcase_01 AC 19 ms
8,660 KB
testcase_02 AC 23 ms
8,696 KB
testcase_03 AC 23 ms
8,624 KB
testcase_04 AC 23 ms
8,564 KB
testcase_05 AC 23 ms
8,572 KB
testcase_06 AC 19 ms
8,544 KB
testcase_07 AC 26 ms
8,552 KB
testcase_08 AC 25 ms
8,692 KB
testcase_09 AC 26 ms
8,624 KB
testcase_10 AC 53 ms
8,660 KB
testcase_11 AC 52 ms
8,592 KB
testcase_12 AC 40 ms
8,564 KB
testcase_13 AC 24 ms
8,684 KB
testcase_14 AC 19 ms
8,612 KB
testcase_15 AC 19 ms
8,560 KB
testcase_16 AC 19 ms
8,668 KB
testcase_17 AC 19 ms
8,660 KB
testcase_18 AC 19 ms
8,668 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque


def min_dict_order(str_deque: deque):
    up_heap(str_deque)
    seq = ''
    while True:
        chr_deque = str_deque[0]
        seq += chr_deque.popleft()
        if not chr_deque:
            str_deque.popleft()
            try:
                str_deque.appendleft(str_deque.pop())
            except IndexError:
                break
        down_heap(str_deque)
    seq = seq.replace('{', '')
    return seq


def initialize():
    N = int(input())
    str_deque = deque(deque(input() + '{') for i in range(N))
    return str_deque


def up_heap(str_deque: deque):
    for index in range(len(str_deque) - 1, -1, -1):
        min_index = get_min_index(str_deque, index)
        if min_index != index:
            swap(str_deque, index, min_index)
            down_heap(str_deque, min_index)


def down_heap(str_deque: deque, index: int=0):
    while True:
        min_index = get_min_index(str_deque, index)
        if min_index == index:
            break
        else:
            swap(str_deque, index, min_index)
            index = min_index


def get_min_index(str_deque, index):
    dct = {index: str_deque[index]}
    try:
        dct.update({2 * index: str_deque[2 * index]})
    except IndexError:
        pass
    try:
        dct.update({2 * index + 1: str_deque[2 * index + 1]})
    except IndexError:
        pass
    return min(dct, key=dct.get)


def swap(seq, i, j):
    tmp = seq[i]
    seq[i] = seq[j]
    seq[j] = tmp


if __name__ == '__main__':
    print(min_dict_order(initialize()))
0