結果

問題 No.433 ICPC国内予選の選抜ルールがこんな感じだったらうれしい
ユーザー lam6er
提出日時 2025-04-15 23:28:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 562 ms / 4,000 ms
コード長 1,502 bytes
コンパイル時間 258 ms
コンパイル使用メモリ 82,276 KB
実行使用メモリ 133,528 KB
最終ジャッジ日時 2025-04-15 23:30:19
合計ジャッジ時間 15,065 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 48
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
from collections import defaultdict

def main():
    import sys
    input = sys.stdin.read
    data = input().split()
    idx = 0
    n = int(data[idx])
    k = int(data[idx+1])
    idx += 2
    
    teams = []
    for original_idx in range(n):
        s = int(data[idx])
        p = int(data[idx+1])
        u = int(data[idx+2])
        teams.append((s, p, u, original_idx))
        idx += 3
    
    groups = defaultdict(list)
    for s, p, u, original_idx in teams:
        groups[u].append((s, p, original_idx))
    
    # Sort each group's teams by descending S, then ascending P
    for u in groups:
        groups[u].sort(key=lambda x: (-x[0], x[1]))
    
    heap = []
    for u in groups:
        if groups[u]:
            s, p, original_idx = groups[u][0]
            heapq.heappush(heap, (-s, 0, p, u, 0))
    
    selected_teams_count = defaultdict(int)
    selected = []
    
    for _ in range(k):
        s_neg, count, p, u, index = heapq.heappop(heap)
        s = -s_neg
        team = groups[u][index]
        original_idx = team[2]
        selected.append(original_idx)
        
        selected_teams_count[u] += 1
        next_index = selected_teams_count[u]
        if next_index < len(groups[u]):
            next_team = groups[u][next_index]
            next_s, next_p, next_original_idx = next_team
            heapq.heappush(heap, (-next_s, next_index, next_p, u, next_index))
    
    for idx in selected:
        print(idx)

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