結果

問題 No.2501 Maximum Inversion Number
ユーザー flygonflygon
提出日時 2023-10-13 23:04:01
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,603 bytes
コンパイル時間 385 ms
コンパイル使用メモリ 86,996 KB
実行使用メモリ 126,952 KB
最終ジャッジ日時 2023-10-13 23:04:07
合計ジャッジ時間 6,139 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
71,776 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 233 ms
126,468 KB
testcase_05 AC 261 ms
126,952 KB
testcase_06 AC 243 ms
126,124 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 140 ms
106,004 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 480 ms
91,408 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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
    mxcnt = defaultdict(int)
    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
        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()
0