結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 145 ms
80,112 KB
testcase_01 AC 239 ms
82,860 KB
testcase_02 AC 343 ms
84,604 KB
testcase_03 AC 241 ms
86,832 KB
testcase_04 AC 285 ms
145,024 KB
testcase_05 AC 262 ms
144,952 KB
testcase_06 AC 290 ms
145,412 KB
testcase_07 AC 219 ms
104,812 KB
testcase_08 AC 247 ms
109,908 KB
testcase_09 AC 195 ms
112,640 KB
testcase_10 AC 259 ms
85,364 KB
testcase_11 AC 268 ms
96,144 KB
testcase_12 AC 270 ms
97,388 KB
testcase_13 AC 162 ms
81,944 KB
testcase_14 AC 265 ms
140,124 KB
testcase_15 AC 307 ms
86,840 KB
testcase_16 AC 645 ms
98,244 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from typing import List

def main(n: 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
    
    tl, tr = 0, 10 ** 9 + 10
    while abs(tr - tl) > 1:
        t = (tl + tr) // 2

        sum_t = 0
        for i in range(n):
            sum_t += max(l[i], min(t, r[i]))

        if sum_t < m:
            tl = t
        else:
            tr = t

    t0, sum_t0 = tl, 0
    for i in range(n):
        sum_t0 += max(l[i], min(t0, r[i]))
    d = m - sum_t0

    x = 0
    for i in range(n):
        c = max(l[i], min(tl, r[i]))
        if d > 0 and l[i] <= t0 < r[i]:
            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