結果

問題 No.335 門松宝くじ
ユーザー gew1fw
提出日時 2025-06-12 16:10:58
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,755 bytes
コンパイル時間 237 ms
コンパイル使用メモリ 82,184 KB
実行使用メモリ 54,144 KB
最終ジャッジ日時 2025-06-12 16:11:04
合計ジャッジ時間 3,943 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 1 TLE * 1 -- * 8
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from itertools import combinations

def main():
    input = sys.stdin.read().split()
    idx = 0
    N, M = int(input[idx]), int(input[idx+1])
    idx += 2
    tickets = []
    for _ in range(M):
        E = list(map(int, input[idx:idx+N]))
        idx += N
        tickets.append(E)
    
    best_ticket = 0
    max_expected = -1

    for ticket_idx, E in enumerate(tickets):
        pos_map = {v: i for i, v in enumerate(E)}
        total = 0
        count = 0
        # Generate all possible (a, b) pairs
        for a, b in combinations(E, 2):
            i = pos_map[a]
            j = pos_map[b]
            current_max = 0
            # Iterate all possible c (not a or b)
            for c in E:
                if c == a or c == b:
                    continue
                k = pos_map[c]
                # Sort the positions to get the order in the ticket
                positions = sorted([i, j, k])
                x = E[positions[0]]
                y = E[positions[1]]
                z = E[positions[2]]
                # Compute median using max and min
                max_val = max(x, y, z)
                min_val = min(x, y, z)
                median = x + y + z - max_val - min_val
                if median == x or median == z:
                    if max_val > current_max:
                        current_max = max_val
            total += current_max
            count += 1
        # Calculate expected value
        expected = total / count if count else 0
        # Update best ticket
        if expected > max_expected or (expected == max_expected and ticket_idx < best_ticket):
            max_expected = expected
            best_ticket = ticket_idx
    print(best_ticket)

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