結果

問題 No.2501 Maximum Inversion Number
ユーザー mkawa2mkawa2
提出日時 2023-10-13 23:32:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 826 ms / 2,000 ms
コード長 1,785 bytes
コンパイル時間 1,383 ms
コンパイル使用メモリ 86,340 KB
実行使用メモリ 124,052 KB
最終ジャッジ日時 2023-10-13 23:32:13
合計ジャッジ時間 6,765 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
71,232 KB
testcase_01 AC 201 ms
77,604 KB
testcase_02 AC 399 ms
79,984 KB
testcase_03 AC 183 ms
81,548 KB
testcase_04 AC 226 ms
121,060 KB
testcase_05 AC 197 ms
120,708 KB
testcase_06 AC 230 ms
121,032 KB
testcase_07 AC 172 ms
103,500 KB
testcase_08 AC 165 ms
106,936 KB
testcase_09 AC 108 ms
92,176 KB
testcase_10 AC 202 ms
80,464 KB
testcase_11 AC 193 ms
99,272 KB
testcase_12 AC 195 ms
102,440 KB
testcase_13 AC 82 ms
76,324 KB
testcase_14 AC 185 ms
124,052 KB
testcase_15 AC 208 ms
81,128 KB
testcase_16 AC 826 ms
93,792 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(200005)
# sys.set_int_max_str_digits(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
# dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
# inf = -1-(-1 << 31)
inf = -1-(-1 << 63)
# md = 10**9+7
md = 998244353

def solve():
    def binary_search(l, r, minimize):
        if minimize: l -= 1
        else: r += 1
        while l+1 < r:
            m = (l+r)//2
            if ok(m) ^ minimize: l = m
            else: r = m
        if minimize: return r
        return l

    def ok(m):
        cnt = 0
        for l, r in zip(ll, rr):
            cnt += max(min(r, m), l)
            if cnt > M: return False
        return True

    n, M = LI()
    ll = LI()
    rr = LI()
    if sum(ll) > M or sum(rr) < M:
        print(-1)
        return
    x = binary_search(0, 10**9, False)
    # print(x)

    s = 0
    aa = []
    for l, r in zip(ll, rr):
        if l > x:
            aa.append(l)
            s += l
        elif r < x:
            aa.append(r)
            s += r
        else:
            aa.append(x)
            s += x

    ans = 0
    for l, r, a in zip(ll, rr, aa):
        if a == x and s < M and r > a:
            a += 1
            s += 1
        ans += (M-a)*a
    print(ans//2)

for _ in range(II()): solve()
0