結果

問題 No.2501 Maximum Inversion Number
ユーザー naesiranaesira
提出日時 2023-10-13 23:41:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,598 ms / 2,000 ms
コード長 767 bytes
コンパイル時間 262 ms
コンパイル使用メモリ 86,440 KB
実行使用メモリ 113,008 KB
最終ジャッジ日時 2023-10-13 23:41:30
合計ジャッジ時間 7,577 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,032 KB
testcase_01 AC 240 ms
78,756 KB
testcase_02 AC 802 ms
82,672 KB
testcase_03 AC 270 ms
82,012 KB
testcase_04 AC 278 ms
113,008 KB
testcase_05 AC 242 ms
107,632 KB
testcase_06 AC 264 ms
107,548 KB
testcase_07 AC 229 ms
97,416 KB
testcase_08 AC 207 ms
104,180 KB
testcase_09 AC 147 ms
91,972 KB
testcase_10 AC 254 ms
81,556 KB
testcase_11 AC 223 ms
93,056 KB
testcase_12 AC 231 ms
92,740 KB
testcase_13 AC 95 ms
76,236 KB
testcase_14 AC 362 ms
110,480 KB
testcase_15 AC 336 ms
81,268 KB
testcase_16 AC 1,598 ms
95,552 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

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