結果

問題 No.545 ママの大事な二人の子供
ユーザー rpy3cpprpy3cpp
提出日時 2017-07-14 23:22:13
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 479 ms / 2,000 ms
コード長 931 bytes
コンパイル時間 88 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 17,408 KB
最終ジャッジ日時 2024-04-16 20:41:53
合計ジャッジ時間 5,936 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
10,752 KB
testcase_01 AC 33 ms
10,752 KB
testcase_02 AC 30 ms
10,752 KB
testcase_03 AC 30 ms
10,752 KB
testcase_04 AC 30 ms
10,752 KB
testcase_05 AC 30 ms
10,752 KB
testcase_06 AC 30 ms
10,880 KB
testcase_07 AC 32 ms
10,752 KB
testcase_08 AC 31 ms
10,752 KB
testcase_09 AC 31 ms
10,752 KB
testcase_10 AC 30 ms
10,752 KB
testcase_11 AC 31 ms
10,752 KB
testcase_12 AC 31 ms
10,752 KB
testcase_13 AC 30 ms
10,880 KB
testcase_14 AC 31 ms
10,880 KB
testcase_15 AC 31 ms
10,752 KB
testcase_16 AC 31 ms
10,752 KB
testcase_17 AC 32 ms
10,880 KB
testcase_18 AC 39 ms
11,008 KB
testcase_19 AC 50 ms
11,136 KB
testcase_20 AC 70 ms
11,520 KB
testcase_21 AC 31 ms
10,880 KB
testcase_22 AC 236 ms
13,952 KB
testcase_23 AC 479 ms
17,280 KB
testcase_24 AC 217 ms
13,568 KB
testcase_25 AC 437 ms
16,896 KB
testcase_26 AC 451 ms
16,896 KB
testcase_27 AC 437 ms
17,280 KB
testcase_28 AC 478 ms
17,408 KB
testcase_29 AC 460 ms
17,408 KB
testcase_30 AC 472 ms
17,152 KB
testcase_31 AC 470 ms
17,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n = int(input())
As = []
Bs = []
for i in range(n):
    a, b = map(int, input().split())
    As.append(a)
    Bs.append(b)

def solve(n, As, Bs):
    n1 = n // 2
    n2 = n - n1
    A1 = As[:n1]
    A2 = As[n1:]
    B1 = Bs[:n1]
    B2 = Bs[n1:]
    lst1 = generate_lst(n1, A1, B1)
    lst2 = generate_lst(n2, B2, A2)
    lst1.sort()
    lst2.sort()
    record = float('inf')
    idx2 = 0
    for d1 in lst1:
        while (idx2 < len(lst2) and d1 - lst2[idx2] > 0):
            record = min(record, abs(d1 - lst2[idx2]))
            idx2 += 1
        if (idx2 < len(lst2)):
            record = min(record, abs(d1 - lst2[idx2]))
    return record

def generate_lst(n, A, B):
    lst = []
    for pat in range(1 << n):
        tmp = 0
        for i in range(n):
            if pat & (1 << i):
                tmp += A[i]
            else:
                tmp -= B[i]
        lst.append(tmp)
    return lst

print(solve(n, As, Bs))
0