結果

問題 No.158 奇妙なお使い
ユーザー rpy3cpprpy3cpp
提出日時 2015-06-02 18:51:21
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 3,789 ms / 5,000 ms
コード長 1,985 bytes
コンパイル時間 148 ms
コンパイル使用メモリ 11,008 KB
実行使用メモリ 170,356 KB
最終ジャッジ日時 2023-09-11 11:25:16
合計ジャッジ時間 6,573 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 17 ms
8,044 KB
testcase_01 AC 27 ms
8,680 KB
testcase_02 AC 17 ms
7,996 KB
testcase_03 AC 16 ms
8,020 KB
testcase_04 AC 3,789 ms
170,356 KB
testcase_05 AC 17 ms
8,080 KB
testcase_06 AC 17 ms
8,116 KB
testcase_07 AC 17 ms
8,180 KB
testcase_08 AC 17 ms
7,988 KB
testcase_09 AC 17 ms
8,208 KB
testcase_10 AC 22 ms
8,596 KB
testcase_11 AC 20 ms
8,368 KB
testcase_12 AC 16 ms
8,048 KB
testcase_13 AC 17 ms
8,084 KB
testcase_14 AC 25 ms
8,732 KB
testcase_15 AC 34 ms
8,848 KB
testcase_16 AC 19 ms
8,464 KB
testcase_17 AC 65 ms
10,636 KB
testcase_18 AC 478 ms
21,900 KB
testcase_19 AC 19 ms
8,384 KB
testcase_20 AC 23 ms
8,780 KB
testcase_21 AC 87 ms
13,424 KB
testcase_22 AC 90 ms
11,944 KB
testcase_23 AC 18 ms
8,076 KB
testcase_24 AC 18 ms
8,148 KB
testcase_25 AC 17 ms
8,152 KB
testcase_26 AC 25 ms
8,464 KB
testcase_27 AC 17 ms
8,076 KB
testcase_28 AC 31 ms
8,640 KB
testcase_29 AC 19 ms
8,476 KB
testcase_30 AC 106 ms
10,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**6)

def read_data():
    a3, a2, a1 = map(int, input().split())
    db = int(input())
    b3, b2, b1 = map(int, input().split())
    dc = int(input())
    c3, c2, c1 = map(int, input().split())
    return a3, a2, a1, b3, b2, b1, c3, c2, c1, db, dc


def solve(a3, a2, a1, b3, b2, b1, c3, c2, c1, db, dc):
    memo = dict()
    dif_b = db - b3*1000 - b2*100 - b1
    dif_c = dc - c3*1000 - c2*100 - c1
    dif = min(dif_b, dif_c)
    record = 0

    def dfs(a3, a2, a1, depth):
        nonlocal record
        nonlocal memo
        if (a3, a2, a1) in memo:
            tmp = memo[a3, a2, a1]
            if tmp + depth > record:
                record = depth + tmp
            return tmp
        if (a3 * 1000 + a2 * 100 + a1) // dif < record - depth:
            return 0
        tmp = 0
        d3, d2, d1 = generate_payment_patterns(a3, a2, a1, db)
        if d3 or d2 or d1:
            t = dfs(a3-d3+b3, a2-d2+b2, a1-d1+b1, depth+1)
            if t > tmp:
                tmp = t
        d3, d2, d1 = generate_payment_patterns(a3, a2, a1, dc)
        if d3 or d2 or d1:
            t = dfs(a3-d3+c3, a2-d2+c2, a1-d1+c1, depth+1)
            if t > tmp:
                tmp = t
        memo[a3, a2, a1] = tmp
        if depth + tmp > record:
            record = depth + tmp
        return tmp + 1

    dfs(a3, a2, a1, 0)
    return record


def generate_payment_patterns(a3, a2, a1, d):
    for d3 in range(d//1000, -1, -1):
        if d3 > a3:
            continue
        for d2 in range((d - d3*1000)//100, -1, -1):
            if d2 > a2:
                continue
            d1 = d - d3*1000 - d2*100
            if d1 > a1:
                continue
            return d3, d2, d1
    return 0, 0, 0


if __name__ == '__main__':
    a3, a2, a1, b3, b2, b1, c3, c2, c1, db, dc = read_data()
    res1 = solve(a3, a2, a1, b3, b2, b1, c3, c2, c1, db, dc)
    res2 = solve(a3, a2, a1, c3, c2, c1, b3, b2, b1, dc, db)
    print(max(res1, res2))
0