結果

問題 No.2501 Maximum Inversion Number
ユーザー suisensuisen
提出日時 2023-07-24 20:21:51
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,137 bytes
コンパイル時間 237 ms
コンパイル使用メモリ 82,312 KB
実行使用メモリ 70,864 KB
最終ジャッジ日時 2024-04-09 18:38:02
合計ジャッジ時間 2,603 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from typing import List

def main(_: int, m: int, l: List[int], r: List[int]):
    if not (sum(l) <= m <= sum(r)):
        return -1
    
    if m == sum(l):
        return (m * m - sum(e * e for e in l)) // 2
    
    def clamp(v: int, l: int, r: int):
        return max(l, min(v, r))
    
    def get_sum(t: int):
        return sum(clamp(t, a, b) for a, b in zip(l, r))
    
    tl, tr = 0, 10 ** 9 + 10
    while abs(tr - tl) > 1:
        t = (tl + tr) // 2
        if get_sum(t) < m:
            tl = t
        else:
            tr = t

    t0, d = tl, m - get_sum(tl)
    x = 0
    for a, b in zip(l, r):
        c = clamp(t0, a, b)
        if d > 0 and a <= t0 < b:
            c += 1
            d -= 1
        x += c * c
    
    return (m * m - x) // 2

if __name__ == '__main__':
    t = int(sys.stdin.readline().rstrip())
    answers = []
    for _ in range(t):
        n, m = map(int, sys.stdin.readline().rstrip())
        l = list(map(int, sys.stdin.readline().rstrip()))
        r = list(map(int, sys.stdin.readline().rstrip()))
        answers.append(main(n, m, l, r))
    print('\n'.join(map(str, answers)))
0