結果

問題 No.545 ママの大事な二人の子供
ユーザー mkawa2
提出日時 2020-05-21 18:27:10
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 166 ms / 2,000 ms
コード長 1,667 bytes
コンパイル時間 100 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 23,376 KB
最終ジャッジ日時 2024-10-02 00:07:11
合計ジャッジ時間 3,423 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 32
権限があれば一括ダウンロードができます

ソースコード

diff #

from operator import itemgetter
from itertools import *
from bisect import *
from collections import *
from heapq import *
import sys

sys.setrecursionlimit(10 ** 6)

def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def SI(): return sys.stdin.readline()[:-1]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
int1 = lambda x: int(x) - 1
def MI1(): return map(int1, sys.stdin.readline().split())
def LI1(): return list(map(int1, sys.stdin.readline().split()))
p2D = lambda x: print(*x, sep="\n")
dij = [(1, 0), (0, 1), (-1, 0), (0, -1)]

# n<=32だから半分全列挙
def main():
    inf=1<<63
    n=II()
    ab=LLI(n)

    # AがBより何点多いかを前後半に分けて全列挙する
    def alldiff(si,ei):
        s=set()
        s.add(0)
        for a,b in ab[si:ei]:
            ns=set()
            for d in s:
                ns.add(d+a)
                ns.add(d-b)
            s=ns
        return list(sorted(s))

    pre=alldiff(0,n//2)
    pos=alldiff(n//2,n)
    #print(pre)
    #print(pos)

    # しゃくとり法で絶対値の最小値を探す
    # 前半(pre)の値一つに対して、後半(pos)の値2つと比較の必要がある
    # 前半の値がa=3で、pos=[-5,1,6]だったら、
    # -5(3と足して負になる),1(3と足して正になる)と比較する
    ans=inf
    j=len(pos)-2
    for i,a in enumerate(pre):
        while j>0 and a+pos[j]>=0:j-=1
        ans=min(ans,abs(a+pos[j]),abs(a+pos[j+1]))
    print(ans)

main()
0