結果

問題 No.775 tatyamと素数大富豪(hard)
ユーザー lam6er
提出日時 2025-04-09 21:06:22
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,272 bytes
コンパイル時間 213 ms
コンパイル使用メモリ 82,876 KB
実行使用メモリ 669,036 KB
最終ジャッジ日時 2025-04-09 21:08:21
合計ジャッジ時間 5,120 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4 WA * 1
other AC * 4 MLE * 1 -- * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

from functools import cmp_to_key
import heapq

class InvertedStr:
    def __init__(self, s):
        self.s = s
    def __lt__(self, other):
        return self.s > other.s

def compare(x, y):
    x_str = str(x)
    y_str = str(y)
    if x_str + y_str > y_str + x_str:
        return -1
    else:
        return 1

n, k = map(int, input().split())
a = list(map(int, input().split()))

sorted_a = sorted(a, key=cmp_to_key(compare))
initial_str = ''.join(map(str, sorted_a))

heap = []
visited = set()

heapq.heappush(heap, (InvertedStr(initial_str), sorted_a))
visited.add(initial_str)

output = []

while len(output) < k and heap:
    inverted_str, current_arr = heapq.heappop(heap)
    current_str = inverted_str.s
    output.append(current_str)

    # Generate all possible swaps
    for i in range(len(current_arr)):
        for j in range(i + 1, len(current_arr)):
            if current_arr[i] != current_arr[j]:
                new_arr = current_arr.copy()
                new_arr[i], new_arr[j] = new_arr[j], new_arr[i]
                new_str = ''.join(map(str, new_arr))
                if new_str not in visited:
                    visited.add(new_str)
                    heapq.heappush(heap, (InvertedStr(new_str), new_arr))

for s in output[:k]:
    print(s)
0