結果

問題 No.545 ママの大事な二人の子供
ユーザー mkawa2mkawa2
提出日時 2020-05-21 18:27:10
言語 Python3
(3.11.6 + numpy 1.26.0 + scipy 1.11.3)
結果
AC  
実行時間 167 ms / 2,000 ms
コード長 1,667 bytes
コンパイル時間 139 ms
コンパイル使用メモリ 11,088 KB
実行使用メモリ 21,276 KB
最終ジャッジ日時 2023-07-25 05:49:56
合計ジャッジ時間 3,514 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
8,656 KB
testcase_01 AC 21 ms
8,676 KB
testcase_02 AC 21 ms
8,736 KB
testcase_03 AC 21 ms
8,728 KB
testcase_04 AC 21 ms
8,672 KB
testcase_05 AC 21 ms
8,852 KB
testcase_06 AC 21 ms
8,676 KB
testcase_07 AC 21 ms
8,732 KB
testcase_08 AC 21 ms
8,676 KB
testcase_09 AC 21 ms
8,676 KB
testcase_10 AC 21 ms
8,676 KB
testcase_11 AC 21 ms
8,740 KB
testcase_12 AC 21 ms
8,804 KB
testcase_13 AC 21 ms
8,740 KB
testcase_14 AC 21 ms
8,752 KB
testcase_15 AC 21 ms
8,860 KB
testcase_16 AC 21 ms
8,808 KB
testcase_17 AC 21 ms
8,736 KB
testcase_18 AC 24 ms
9,096 KB
testcase_19 AC 26 ms
9,200 KB
testcase_20 AC 34 ms
10,372 KB
testcase_21 AC 21 ms
8,768 KB
testcase_22 AC 91 ms
15,536 KB
testcase_23 AC 165 ms
21,236 KB
testcase_24 AC 87 ms
15,124 KB
testcase_25 AC 156 ms
20,508 KB
testcase_26 AC 160 ms
20,632 KB
testcase_27 AC 159 ms
21,276 KB
testcase_28 AC 164 ms
21,196 KB
testcase_29 AC 159 ms
21,276 KB
testcase_30 AC 167 ms
20,920 KB
testcase_31 AC 166 ms
20,920 KB
権限があれば一括ダウンロードができます

ソースコード

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