結果

問題 No.2501 Maximum Inversion Number
ユーザー naesiranaesira
提出日時 2023-10-13 23:09:46
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 644 bytes
コンパイル時間 292 ms
コンパイル使用メモリ 86,272 KB
実行使用メモリ 112,648 KB
最終ジャッジ日時 2023-10-13 23:10:04
合計ジャッジ時間 8,517 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
70,888 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 230 ms
98,288 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

def bis(ok, ng):
  def is_ok(md):
    ttl = sum(min(max(L[i], md), R[i]) for i in range(n))
    return ttl <= m
  
  while abs(ok - ng) > 1:
    md = (ng + ok) // 2
    if is_ok(md): ok = md
    else: ng = md
  return ok

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()))

  if m < sum(L) or sum(R) < m:
    print(-1)
    continue
  
  k = max(R) if sum(R) == m else bis(min(L), max(R))
  cnt = [min(max(L[i], k), R[i]) for i in range(n)]
  ans = sum(cnt[i] * (m - cnt[i]) for i in range(n))
  ans += (m - sum(cnt)) * (m - k - 1)
  print(ans // 2)
0