結果
| 問題 |
No.297 カードの数式
|
| コンテスト | |
| ユーザー |
gew1fw
|
| 提出日時 | 2025-06-12 15:05:30 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,635 bytes |
| コンパイル時間 | 366 ms |
| コンパイル使用メモリ | 82,652 KB |
| 実行使用メモリ | 59,264 KB |
| 最終ジャッジ日時 | 2025-06-12 15:06:23 |
| 合計ジャッジ時間 | 3,111 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 1 TLE * 1 -- * 21 |
ソースコード
import itertools
n = int(input())
cards = input().split()
digits = [c for c in cards if c.isdigit()]
ops = [c for c in cards if c in '+-']
d = len(digits)
o = len(ops)
k = o + 1
def generate_partitions(d, k):
if k == 1:
return [(d,)]
partitions = []
for first in range(1, d - (k-1) + 1):
for rest in generate_partitions(d - first, k-1):
partitions.append((first,) + rest)
return partitions
partitions = generate_partitions(d, k)
max_val = -float('inf')
min_val = float('inf')
for partition in partitions:
for num_perm in itertools.permutations(digits):
numbers = []
index = 0
valid = True
for p in partition:
if index + p > len(num_perm):
valid = False
break
part = num_perm[index:index+p]
num_str = ''.join(part)
numbers.append(int(num_str))
index += p
if not valid:
continue
seen_ops = set()
for op_perm in itertools.permutations(ops):
if op_perm not in seen_ops:
seen_ops.add(op_perm)
if len(numbers) != len(op_perm) + 1:
continue
current = numbers[0]
for i in range(len(op_perm)):
if op_perm[i] == '+':
current += numbers[i+1]
else:
current -= numbers[i+1]
if current > max_val:
max_val = current
if current < min_val:
min_val = current
print(max_val, min_val)
gew1fw