結果

問題 No.507 ゲーム大会(チーム決め)
ユーザー rpy3cpp
提出日時 2017-04-23 23:12:30
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 536 ms / 3,000 ms
コード長 1,014 bytes
コンパイル時間 208 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 15,296 KB
最終ジャッジ日時 2024-07-22 10:11:58
合計ジャッジ時間 5,683 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

def read_data():
    N, M = map(int, input().split())
    a0 = int(input())
    As = [int(input()) for _ in range(N - 1)]
    return N, M, a0, As


def solve(N, M, a0, As):
    As.sort(reverse=True)
    rank_best = calc_rank(a0, 0, As)
    if rank_best > M:
        return -1
    else:
        OK = 0
    rank_worst = calc_rank(a0, len(As) - 1, As)
    if rank_worst <= M:
        return As[-1]
    else:
        NG = len(As) - 1
    while OK < NG - 1:
        mid = (OK + NG) // 2
        rank = calc_rank(a0, mid, As)
        if rank <= M:
            OK = mid
        else:
            NG = mid
    return As[OK]


def calc_rank(a0, pos, As):
    score = a0 + As[pos]
    j = len(As) - 1
    rank = 1
    for i in range(len(As)):
        if i == pos: continue
        if i >= j: break
        ai = As[i]
        while (ai + As[j] <= score) or j == pos:
            j -= 1
            if i >= j: return rank
        rank += 1
        j -= 1
    return rank

N, M, a0, As = read_data()
print(solve(N, M, a0, As))
0