結果

問題 No.2501 Maximum Inversion Number
ユーザー naesiranaesira
提出日時 2023-10-13 23:03:42
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 769 bytes
コンパイル時間 354 ms
コンパイル使用メモリ 87,008 KB
実行使用メモリ 117,164 KB
最終ジャッジ日時 2023-10-13 23:03:55
合計ジャッジ時間 12,361 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,240 KB
testcase_01 AC 492 ms
82,180 KB
testcase_02 AC 818 ms
81,180 KB
testcase_03 AC 595 ms
95,468 KB
testcase_04 AC 702 ms
117,164 KB
testcase_05 AC 790 ms
114,184 KB
testcase_06 AC 766 ms
108,948 KB
testcase_07 AC 518 ms
109,156 KB
testcase_08 AC 608 ms
103,568 KB
testcase_09 AC 129 ms
93,232 KB
testcase_10 AC 468 ms
94,860 KB
testcase_11 AC 575 ms
105,644 KB
testcase_12 AC 546 ms
105,468 KB
testcase_13 AC 98 ms
75,996 KB
testcase_14 AC 550 ms
115,884 KB
testcase_15 AC 572 ms
96,968 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 = sorted([min(max(L[i], k), R[i]), i] for i in range(n))
  ttl = sum(cnt[i][0] for i in range(n))
  i = 0
  while ttl < m:
    if cnt[i][0] < R[cnt[i][1]]:
      cnt[i][0] += 1
      ttl += 1
    i += 1
  ans = sum(cnt[i][0] * (m - cnt[i][0]) for i in range(n)) // 2
  print(ans)
0