結果

問題 No.297 カードの数式
ユーザー Coki628Coki628
提出日時 2020-09-24 23:22:23
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,145 bytes
コンパイル時間 365 ms
コンパイル使用メモリ 87,352 KB
実行使用メモリ 169,872 KB
最終ジャッジ日時 2023-09-10 14:09:29
合計ジャッジ時間 4,608 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
75,992 KB
testcase_01 AC 74 ms
71,676 KB
testcase_02 AC 75 ms
71,436 KB
testcase_03 AC 72 ms
71,776 KB
testcase_04 RE -
testcase_05 AC 72 ms
71,592 KB
testcase_06 AC 74 ms
71,636 KB
testcase_07 AC 73 ms
71,752 KB
testcase_08 AC 73 ms
71,732 KB
testcase_09 AC 72 ms
71,800 KB
testcase_10 AC 77 ms
71,836 KB
testcase_11 AC 74 ms
71,692 KB
testcase_12 RE -
testcase_13 TLE -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c for j in range(b)] for i in range(a)]
def list3d(a, b, c, d): return [[[d for k in range(c)] for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e for l in range(d)] for k in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10**9)
INF = 10**18
MOD = 10**9 + 7
EPS = 10**-10

N = INT()

A = []
add = sub = 0
for s in input().split():
    if s == '+':
        add += 1
    elif s == '-':
        sub += 1
    else:
        A.append(s)
A.sort(reverse=1)

N = len(A)
blank = N - add - sub - 1
B = [' ']*blank + ['+']*add + ['-']*sub
M = len(B)

memo = {}
useda = [0] * N
usedb = [0] * M
def rec1(s):
    i = len(s)
    if s in memo:
        return memo[s]
    if i == N+M:
        return eval(s.replace(' ', ''))
    res = INF
    if i % 2 == 0:
        for j in range(N):
            if not useda[j]:
                useda[j] = 1
                res = min(res, rec1(s + A[j]))
                useda[j] = 0
    else:
        for j in range(M):
            if not usedb[j]:
                usedb[j] = 1
                res = min(res, rec1(s + B[j]))
                usedb[j] = 0
    memo[s] = res
    return res
mn = rec1('')

memo = {}
useda = [0] * N
usedb = [0] * M
def rec2(s):
    i = len(s)
    if s in memo:
        return memo[s]
    if i == N+M:
        return eval(s.replace(' ', ''))
    res = -INF
    if i % 2 == 0:
        for j in range(N):
            if not useda[j]:
                useda[j] = 1
                res = max(res, rec2(s + A[j]))
                useda[j] = 0
    else:
        for j in range(M):
            if not usedb[j]:
                usedb[j] = 1
                res = max(res, rec2(s + B[j]))
                usedb[j] = 0
    memo[s] = res
    return res
mx = rec2('')

print(mx, mn)
0