結果

問題 No.3583 二部マッチング最適化
コンテスト
ユーザー kidodesu
提出日時 2026-07-04 17:22:27
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 63 ms / 6,000 ms
コード長 1,482 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 467 ms
コンパイル使用メモリ 84,992 KB
実行使用メモリ 74,880 KB
最終ジャッジ日時 2026-07-04 17:23:06
合計ジャッジ時間 3,693 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 44
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

from heapq import *
def main():
    n, m, l = list(map(int, input().split()))
    A = []
    N = 10**3+1
    for a in list(map(int, input().split())):
        A.append(a+N)
    for a in list(map(int, input().split())):
        A.append(-a-N)
    A.sort(key = lambda x: abs(x))
    B = [0] * (n+m+1)
    for i in range(n+m):
        B[i+1] = B[i]+A[i]
    E = [-1]*(n+m+1)
    inf = 1 << 60
    dp = [inf] * (n+m+1)
    dp[0] = 0
    now = 0
    E[0] = 0
    for i in range(1, n+m+1):
        a = A[i-1]
        if 0 < a:
            now += 1
        else:
            now -= 1
        p = E[now]
        if p == -1:
            dp[i] = 0
        else:
            dp[i] = dp[p] + abs(B[i]-B[p])
        E[now] = i
    hq = []
    LR = [[i-1, i+1] for i in range(n+m)]
    for i in range(n+m-1):
        if A[i] * A[i+1] < 0:
            heappush(hq, (abs(A[i]+A[i+1]), i, i+1))
    ans = 0
    used = set()
    for _ in range(l):
        while 1:
            cost, i0, i1 = heappop(hq)
            if LR[i0][1] != i1 or LR[i1][0] != i0 or i0 in used or i1 in used:
                pass
            else:
                break
        ans += cost
        used.add(i0)
        used.add(i1)
        j0, j1 = LR[i0][0], LR[i1][1]
        if 0 <= j0 < n+m:
            LR[j0][1] = j1
        if 0 <= j1 < n+m:
            LR[j1][0] = j0
        if 0 <= j0 and j1 < n+m and A[j0]*A[j1] < 0:
            heappush(hq, (dp[j1+1]-dp[j0]-dp[j1]+dp[j0+1], j0, j1))
    return ans

print(main())
0