結果
問題 | No.545 ママの大事な二人の子供 |
ユーザー |
![]() |
提出日時 | 2020-05-21 18:23:55 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
RE
|
実行時間 | - |
コード長 | 1,667 bytes |
コンパイル時間 | 151 ms |
コンパイル使用メモリ | 12,544 KB |
実行使用メモリ | 23,508 KB |
最終ジャッジ日時 | 2024-10-02 00:06:54 |
合計ジャッジ時間 | 3,044 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 22 RE * 10 |
ソースコード
from operator import itemgetterfrom itertools import *from bisect import *from collections import *from heapq import *import syssys.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) - 1def 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<<63n=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=nsreturn 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=infj=len(pos)-1for i,a in enumerate(pre):while j>0 and a+pos[j]>=0:j-=1ans=min(ans,abs(a+pos[j]),abs(a+pos[j+1]))print(ans)main()