結果

問題 No.2501 Maximum Inversion Number
ユーザー chineristACchineristAC
提出日時 2023-10-13 21:42:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,184 ms / 2,000 ms
コード長 1,975 bytes
コンパイル時間 282 ms
コンパイル使用メモリ 87,104 KB
実行使用メモリ 139,956 KB
最終ジャッジ日時 2023-10-13 21:42:21
合計ジャッジ時間 8,327 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 114 ms
73,228 KB
testcase_01 AC 297 ms
80,916 KB
testcase_02 AC 545 ms
83,308 KB
testcase_03 AC 350 ms
87,020 KB
testcase_04 AC 411 ms
138,900 KB
testcase_05 AC 430 ms
139,420 KB
testcase_06 AC 419 ms
138,668 KB
testcase_07 AC 336 ms
107,492 KB
testcase_08 AC 356 ms
111,456 KB
testcase_09 AC 165 ms
107,288 KB
testcase_10 AC 283 ms
85,556 KB
testcase_11 AC 336 ms
110,264 KB
testcase_12 AC 327 ms
104,640 KB
testcase_13 AC 147 ms
78,372 KB
testcase_14 AC 310 ms
139,956 KB
testcase_15 AC 355 ms
86,912 KB
testcase_16 AC 1,184 ms
101,356 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 brute(n,m,L,R):
    if m < sum(L)  or sum(R) < m:
        return -1
    
    cnt = [L[i] for i in range(n)]

    while sum(cnt) < m:
        mini = 10**10
        arg = -1
        for i in range(n):
            if cnt[i]!=R[i] and cnt[i] < mini:
                mini = cnt[i]
                arg = i
        cnt[arg]  += 1
    
    res = m * (m-1)//2
    for i in range(n):
        res -= cnt[i] * (cnt[i]-1) //2
    return res


    

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 idx:
        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

while False:
    n = random.randint(1,10)
    m = random.randint(1,100)
    L = [random.randint(1,m) for i in range(n)]
    R = [random.randint(L[i],m) for i in range(n)]

    

    if solve(n,m,L,R) != brute(n,m,L,R):
        print(n,m)
        print(L)
        print(R)
        print(solve(n,m,L,R),brute(n,m,L,R))
        exit()
    print("AC")

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

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