結果

問題 No.775 tatyamと素数大富豪(hard)
ユーザー qwewe
提出日時 2025-05-14 12:54:25
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,667 bytes
コンパイル時間 327 ms
コンパイル使用メモリ 82,336 KB
実行使用メモリ 859,688 KB
最終ジャッジ日時 2025-05-14 12:55:45
合計ジャッジ時間 6,838 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4 WA * 1
other AC * 4 MLE * 1 -- * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
from functools import cmp_to_key

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

def main():
    import sys
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx])
    K = int(input[idx+1])
    idx += 2
    a = input[idx:idx+N]
    idx += N
    
    # Sort the cards based on the concatenated comparison in descending order
    a.sort(key=cmp_to_key(lambda x, y: -1 if (x + y) > (y + x) else 1))
    
    initial_tpl = tuple(a)
    initial_s = ''.join(initial_tpl)
    
    heap = []
    heapq.heappush(heap, MaxHeapElement(initial_s, initial_tpl))
    seen = set()
    seen.add(initial_s)
    
    result = []
    
    while len(result) < K and heap:
        current_elem = heapq.heappop(heap)
        current_s = current_elem.s
        current_tpl = current_elem.tpl
        result.append(current_s)
        
        n = len(current_tpl)
        current_lst = list(current_tpl)
        for i in range(n):
            for j in range(i + 1, n):
                if current_lst[i] == current_lst[j]:
                    continue
                # Create new tuple by swapping i and j
                new_lst = list(current_lst)
                new_lst[i], new_lst[j] = new_lst[j], new_lst[i]
                new_tpl = tuple(new_lst)
                new_s = ''.join(new_tpl)
                if new_s < current_s and new_s not in seen:
                    seen.add(new_s)
                    heapq.heappush(heap, MaxHeapElement(new_s, new_tpl))
    
    for s in result[:K]:
        print(s)

if __name__ == '__main__':
    main()
0