結果

問題 No.2501 Maximum Inversion Number
ユーザー naesiranaesira
提出日時 2023-10-13 23:15:58
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 728 bytes
コンパイル時間 388 ms
コンパイル使用メモリ 86,972 KB
実行使用メモリ 112,888 KB
最終ジャッジ日時 2023-10-13 23:16:08
合計ジャッジ時間 8,338 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
70,960 KB
testcase_01 AC 251 ms
78,568 KB
testcase_02 AC 912 ms
83,816 KB
testcase_03 AC 272 ms
82,056 KB
testcase_04 AC 265 ms
112,888 KB
testcase_05 AC 248 ms
108,064 KB
testcase_06 AC 270 ms
108,404 KB
testcase_07 AC 233 ms
98,200 KB
testcase_08 AC 216 ms
105,496 KB
testcase_09 AC 143 ms
92,844 KB
testcase_10 AC 254 ms
81,400 KB
testcase_11 AC 223 ms
93,496 KB
testcase_12 AC 232 ms
93,212 KB
testcase_13 AC 94 ms
75,960 KB
testcase_14 AC 348 ms
112,676 KB
testcase_15 AC 373 ms
81,664 KB
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)]
  ttl = sum(cnt)
  for i in range(n):
    if ttl == m: break
    if cnt[i] == k < R[i]:
      cnt[i] += 1
      ttl += 1
  ans = sum(cnt[i] * (m - cnt[i]) for i in  range(n))
  print(ans // 2)
0