結果

問題 No.2501 Maximum Inversion Number
ユーザー naesiranaesira
提出日時 2024-04-20 13:10:46
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 650 bytes
コンパイル時間 203 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 112,284 KB
最終ジャッジ日時 2024-04-20 13:10:56
合計ジャッジ時間 7,950 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
52,864 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 204 ms
97,896 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 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)]*N
  for i in range(N):
    K[i] = min(max(L[i], K[i]), R[i])
  S = sum(K)
  for i in range(N):
    if S == M: break
    if R[i] <= K[i] < R[i]:
    	K[i] += 1; S += 1
  
  print(M*(M-1)//2 - sum(k*(k-1)//2 for k in K))
0