結果
| 問題 |
No.2501 Maximum Inversion Number
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-10-13 23:42:35 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,212 bytes |
| コンパイル時間 | 309 ms |
| コンパイル使用メモリ | 82,304 KB |
| 実行使用メモリ | 116,272 KB |
| 最終ジャッジ日時 | 2024-09-15 19:35:08 |
| 合計ジャッジ時間 | 5,214 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 4 WA * 13 |
ソースコード
import sys
sys.setrecursionlimit(5*10**5)
input = sys.stdin.readline
from collections import defaultdict, deque, Counter
from heapq import heappop, heappush
from bisect import bisect_left, bisect_right
from math import gcd
def sol():
n,m = map(int,input().split())
l = list(map(int,input().split()))
r = list(map(int,input().split()))
if sum(l) > m or sum(r) < m:
print(-1)
return
cnt = defaultdict(int)
left = m
for i in range(n):
cnt[l[i]] += 1
left -= l[i]
r.sort()
r = r[::-1]
all = []
for k, v in cnt.items():
all.append([k,v])
nn = len(all)
all.sort()
all.append([10**10,0])
ans = []
for i in range(nn):
now = all[i][0]
cnt = all[i][1]
while r and r[-1] == now:
r.pop()
cnt -= 1
ans.append([now, 1])
nxt = all[i+1][0]
while r and r[-1] < nxt:
num = r.pop()
dif = num - now
if cnt == 0: continue
if left > dif * cnt:
left -= dif*cnt
cnt -= 1
now = num
ans.append([now, 1])
else:
add = left//cnt
rr = left % cnt
ans.append([now+add+1,rr])
ans.append([now+add,cnt-rr])
left = 0
cnt = 0
dif = nxt - now
if cnt:
if left > dif * cnt:
left -= dif*cnt
else:
add = left//cnt
rr = left % cnt
ans.append([now+add+1,rr])
ans.append([now+add,cnt-rr])
left = 0
if now + add + 1 == nxt:
cnt = rr
ans.append([now+add,cnt-rr])
elif now + add + 1 == nxt:
cnt -= rr
else:
cnt = 0
ans.append([now+add+1,rr])
ans.append([now+add,cnt-rr])
all[i+1][1] += cnt
ansnum = m*(m-1)//2
for i,j in ans:
ansnum -= j*(i*(i-1)//2)
print(ansnum)
return
T = int(input())
for i in range(T):
sol()