結果

問題 No.2501 Maximum Inversion Number
ユーザー suisensuisen
提出日時 2023-07-24 20:33:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,950 ms / 2,000 ms
コード長 1,186 bytes
コンパイル時間 374 ms
コンパイル使用メモリ 87,172 KB
実行使用メモリ 145,428 KB
最終ジャッジ日時 2023-10-13 18:05:11
合計ジャッジ時間 10,653 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 172 ms
79,928 KB
testcase_01 AC 442 ms
83,340 KB
testcase_02 AC 763 ms
87,048 KB
testcase_03 AC 430 ms
88,724 KB
testcase_04 AC 494 ms
145,044 KB
testcase_05 AC 464 ms
145,428 KB
testcase_06 AC 530 ms
145,220 KB
testcase_07 AC 289 ms
103,004 KB
testcase_08 AC 399 ms
104,176 KB
testcase_09 AC 219 ms
112,156 KB
testcase_10 AC 463 ms
86,908 KB
testcase_11 AC 480 ms
101,460 KB
testcase_12 AC 467 ms
94,376 KB
testcase_13 AC 210 ms
82,304 KB
testcase_14 AC 475 ms
139,944 KB
testcase_15 AC 494 ms
87,500 KB
testcase_16 AC 1,950 ms
103,692 KB
権限があれば一括ダウンロードができます

ソースコード

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, l[i], r[i]) for i in range(n))
    
    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 i in range(n):
        a, b = l[i], r[i]
        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().split())
        l = list(map(int, sys.stdin.readline().rstrip().split()))
        r = list(map(int, sys.stdin.readline().rstrip().split()))
        answers.append(main(n, m, l, r))
    print('\n'.join(map(str, answers)))
0