結果

問題 No.2501 Maximum Inversion Number
ユーザー naesiranaesira
提出日時 2024-04-20 13:27:18
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 633 bytes
コンパイル時間 316 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 112,204 KB
最終ジャッジ日時 2024-04-20 13:27:32
合計ジャッジ時間 8,566 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 44 ms
52,480 KB
testcase_01 AC 238 ms
77,496 KB
testcase_02 AC 924 ms
80,912 KB
testcase_03 AC 258 ms
81,536 KB
testcase_04 AC 236 ms
111,100 KB
testcase_05 AC 278 ms
112,104 KB
testcase_06 AC 268 ms
112,204 KB
testcase_07 AC 219 ms
98,424 KB
testcase_08 AC 255 ms
104,780 KB
testcase_09 AC 113 ms
94,208 KB
testcase_10 AC 266 ms
79,660 KB
testcase_11 AC 213 ms
92,880 KB
testcase_12 AC 220 ms
92,160 KB
testcase_13 AC 71 ms
67,200 KB
testcase_14 AC 347 ms
111,800 KB
testcase_15 AC 409 ms
83,192 KB
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)
  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*(M-1)//2 - sum(k*(k-1)//2 for k in K))
0