結果

問題 No.2501 Maximum Inversion Number
ユーザー suisensuisen
提出日時 2023-07-12 19:09:36
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 996 bytes
コンパイル時間 304 ms
コンパイル使用メモリ 82,316 KB
実行使用メモリ 137,592 KB
最終ジャッジ日時 2024-04-09 18:37:56
合計ジャッジ時間 10,520 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 66 ms
68,136 KB
testcase_01 AC 336 ms
79,596 KB
testcase_02 AC 772 ms
80,844 KB
testcase_03 AC 332 ms
83,608 KB
testcase_04 AC 391 ms
134,644 KB
testcase_05 AC 366 ms
134,860 KB
testcase_06 AC 398 ms
135,840 KB
testcase_07 AC 197 ms
104,380 KB
testcase_08 AC 312 ms
106,276 KB
testcase_09 AC 121 ms
107,364 KB
testcase_10 AC 344 ms
83,620 KB
testcase_11 AC 368 ms
95,496 KB
testcase_12 AC 374 ms
93,532 KB
testcase_13 AC 109 ms
78,380 KB
testcase_14 AC 365 ms
137,592 KB
testcase_15 AC 429 ms
84,028 KB
testcase_16 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

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(input())
    for _ in range(t):
        n, m = map(int, input().split())
        l = list(map(int, input().split()))
        r = list(map(int, input().split()))
        print(main(n, m, l, r))
0