結果

問題 No.2501 Maximum Inversion Number
ユーザー chineristACchineristAC
提出日時 2023-10-13 21:37:47
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,181 bytes
コンパイル時間 262 ms
コンパイル使用メモリ 87,192 KB
実行使用メモリ 139,860 KB
最終ジャッジ日時 2023-10-13 21:37:57
合計ジャッジ時間 8,019 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 109 ms
73,676 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 377 ms
111,432 KB
testcase_09 AC 165 ms
106,880 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 127 ms
78,528 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 AC 1,039 ms
101,304 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from itertools import permutations
import heapq
from collections import deque
import random

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

def solve(n,m,L,R):
    if m < sum(L)  or sum(R) < m:
        return -1
    
    cnt = [L[i] for i in range(n)]
    rest = m - sum(cnt)

    def cond(k):
        check = 0
        for i in range(n):
            if L[i] <= k:
                check += min(k,R[i]) - L[i]
        return check <= rest
    
    ok = min(L)
    ng = max(R) + 1
    while ng-ok>1:
        mid = (ok+ng)//2
        if cond(mid):
            ok = mid
        else:
            ng = mid
    
    
    
    for i in range(n):
        if L[i] <= ok:
            cnt[i] = min(R[i],ok)
    
    
    rest = m - sum(cnt)
    idx = [i for i in range(n)]
    idx.sort(key=lambda i:cnt[i])
    for i in range(n):
        if rest and cnt[i]!=R[i]:
            cnt[i] += 1
            rest -= 1
    
    res = m * (m-1)//2
    for i in range(n):
        res -= cnt[i] * (cnt[i]-1) //2
    
    return res

for _ in range(int(input())):
    n,m = mi()
    L = li()
    R = li()

    print(solve(n,m,L,R))
0