結果

問題 No.2501 Maximum Inversion Number
ユーザー naesiranaesira
提出日時 2024-04-20 13:52:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,264 ms / 2,000 ms
コード長 667 bytes
コンパイル時間 152 ms
コンパイル使用メモリ 82,336 KB
実行使用メモリ 111,928 KB
最終ジャッジ日時 2024-04-20 13:52:39
合計ジャッジ時間 6,118 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,224 KB
testcase_01 AC 190 ms
77,516 KB
testcase_02 AC 622 ms
81,192 KB
testcase_03 AC 234 ms
80,896 KB
testcase_04 AC 235 ms
111,748 KB
testcase_05 AC 213 ms
111,908 KB
testcase_06 AC 232 ms
111,928 KB
testcase_07 AC 187 ms
97,540 KB
testcase_08 AC 214 ms
103,624 KB
testcase_09 AC 100 ms
92,020 KB
testcase_10 AC 210 ms
80,256 KB
testcase_11 AC 187 ms
91,776 KB
testcase_12 AC 182 ms
91,424 KB
testcase_13 AC 60 ms
66,432 KB
testcase_14 AC 298 ms
109,804 KB
testcase_15 AC 298 ms
83,520 KB
testcase_16 AC 1,264 ms
99,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

def binary_search(ok, ng):
  while abs(ok - ng) > 1:
    md = (ng + ok) // 2
    S = sum(min(max(l, md), r) for l, r in zip(L, R))
    if S <= M: ok = md
    else: ng = md
  return ok

for _ in range(T := int(input())):
  N, M = map(int, input().split())
  L = list(map(int, input().split()))
  R = list(map(int, input().split()))
  
  if not sum(L) <= M <= sum(R):
    print(-1); continue

  k = binary_search(min(L), max(R)+1)
  K = [min(max(l, k), r) for l, r in zip(L, R)]
  S = sum(K)
  for i in range(N):
    if S == M: break
    if K[i] == k < R[i]:
      K[i] += 1; S += 1
  
  print((M**2 - sum(k**2 for k in K)) // 2)
0