結果

問題 No.335 門松宝くじ
ユーザー lam6er
提出日時 2025-04-15 23:04:10
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,812 bytes
コンパイル時間 238 ms
コンパイル使用メモリ 81,972 KB
実行使用メモリ 76,764 KB
最終ジャッジ日時 2025-04-15 23:06:22
合計ジャッジ時間 4,676 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 4 TLE * 1 -- * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def main():
    input = sys.stdin.read().split()
    ptr = 0
    N = int(input[ptr])
    M = int(input[ptr+1])
    ptr += 2
    tickets = []
    for _ in range(M):
        E = list(map(int, input[ptr:ptr+N]))
        ptr += N
        tickets.append(E)
    
    best_idx = 0
    best_avg = -1.0
    
    for idx, ticket in enumerate(tickets):
        sorted_values = sorted(ticket, reverse=True)
        pos = {v: i for i, v in enumerate(ticket)}
        total = 0.0
        count = 0
        
        # Iterate over all pairs (a, b)
        for a in range(1, N + 1):
            for b in range(a + 1, N + 1):
                prize = 0
                # Iterate through possible z in descending order
                for z in sorted_values:
                    if z == a or z == b:
                        continue
                    pos_a = pos[a]
                    pos_b = pos[b]
                    pos_z = pos[z]
                    valid = False
                    # Check each permutation's order and Kadomatsu condition
                    # Permutation a, b, z
                    if pos_a < pos_b < pos_z:
                        s = sorted([a, b, z])
                        m = s[1]
                        if m == a or m == z:
                            valid = True
                    if not valid and pos_a < pos_z < pos_b:
                        s = sorted([a, z, b])
                        m = s[1]
                        if m == a or m == b:
                            valid = True
                    if not valid and pos_b < pos_a < pos_z:
                        s = sorted([b, a, z])
                        m = s[1]
                        if m == b or m == z:
                            valid = True
                    if not valid and pos_b < pos_z < pos_a:
                        s = sorted([b, z, a])
                        m = s[1]
                        if m == b or m == a:
                            valid = True
                    if not valid and pos_z < pos_a < pos_b:
                        s = sorted([z, a, b])
                        m = s[1]
                        if m == z or m == b:
                            valid = True
                    if not valid and pos_z < pos_b < pos_a:
                        s = sorted([z, b, a])
                        m = s[1]
                        if m == z or m == a:
                            valid = True
                    if valid:
                        prize = max(a, b, z)
                        break
                total += prize
                count += 1
        avg = total / count if count > 0 else 0.0
        if avg > best_avg or (avg == best_avg and idx < best_idx):
            best_avg = avg
            best_idx = idx
    print(best_idx)

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