結果

問題 No.120 傾向と対策:門松列(その1)
ユーザー FromBooskaFromBooska
提出日時 2023-04-08 06:59:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 521 ms / 5,000 ms
コード長 1,082 bytes
コンパイル時間 161 ms
コンパイル使用メモリ 82,296 KB
実行使用メモリ 82,836 KB
最終ジャッジ日時 2024-04-14 06:39:57
合計ジャッジ時間 3,228 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 521 ms
82,836 KB
testcase_01 AC 505 ms
81,056 KB
testcase_02 AC 375 ms
81,424 KB
testcase_03 AC 490 ms
81,244 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# 同じ長さでない3本を選べばいい
# 長さごとに本数を数えて、本数降順として使うか
# それで間に合うか? N<100なので間に合うかも
# すべてWA出た、本数降順から取るアルゴリズムが間違っている
# できるだけ平均的に取る必要ある
# 最後が長さAが2本、長さBが2本、長さC以後が0本となったらアウトだ

T = int(input())
for t in range(T):

    N = int(input())
    L = list(map(int, input().split()))
    from collections import Counter
    counted = Counter(L)

    #print(counted)
    #print(type(counted))

    C = []
    for n, c in counted.items():
        C.append([-c, n])

    from heapq import *
    heapify(C)

    #print(C)

    count = 0
    while len(C) >= 3:
        count += 1
        c1, n1 = heappop(C)
        c2, n2 = heappop(C)
        c3, n3 = heappop(C)
        if c1+1 < 0:
            heappush(C, [c1+1, n1])
        if c2+1 < 0:
            heappush(C, [c2+1, n2])
        if c3+1 < 0:
            heappush(C, [c3+1, n3])
        #print(C)
    print(count)
0