結果

問題 No.2501 Maximum Inversion Number
ユーザー naesiranaesira
提出日時 2024-04-20 13:28:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,487 ms / 2,000 ms
コード長 671 bytes
コンパイル時間 258 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 112,440 KB
最終ジャッジ日時 2024-04-20 13:28:42
合計ジャッジ時間 6,941 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
51,840 KB
testcase_01 AC 224 ms
77,284 KB
testcase_02 AC 720 ms
81,204 KB
testcase_03 AC 268 ms
81,280 KB
testcase_04 AC 236 ms
111,224 KB
testcase_05 AC 246 ms
111,848 KB
testcase_06 AC 261 ms
112,440 KB
testcase_07 AC 208 ms
97,404 KB
testcase_08 AC 255 ms
103,196 KB
testcase_09 AC 107 ms
91,392 KB
testcase_10 AC 231 ms
80,000 KB
testcase_11 AC 210 ms
91,520 KB
testcase_12 AC 210 ms
91,608 KB
testcase_13 AC 72 ms
66,560 KB
testcase_14 AC 348 ms
109,928 KB
testcase_15 AC 317 ms
83,276 KB
testcase_16 AC 1,487 ms
99,156 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*(M-1)//2 - sum(k*(k-1)//2 for k in K))
0