結果

問題 No.2927 Reverse Polish Equation
ユーザー rlangevin
提出日時 2024-10-31 08:56:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 699 ms / 2,000 ms
コード長 878 bytes
コンパイル時間 770 ms
コンパイル使用メモリ 82,308 KB
実行使用メモリ 132,228 KB
最終ジャッジ日時 2024-10-31 08:56:54
合計ジャッジ時間 14,489 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

Q, Y = map(int, input().split())
S = list(input().split())

def f(x):
    stack = []
    for s in S:
        if s == "+":
            a = stack.pop()
            b = stack.pop()
            stack.append(a+b)
        elif s == "max":
            a = stack.pop()
            b = stack.pop()
            stack.append(max(a,b))
        elif s == "min":
            a = stack.pop()
            b = stack.pop()
            stack.append(min(a,b))
        elif s == "X":
            stack.append(x)
        else:
            stack.append(int(s))            
    
    return stack[-1]
        
            

def BinarySearch(yes = 10 ** 18, no = -1):
    while abs(yes - no) != 1:
        mid = (yes + no)//2
        if f(mid) >= Y:
            yes = mid
        else:
            no = mid
    return yes

ans = BinarySearch()
print(ans) if ans <= 10 ** 15 and f(ans) == Y else print(-1)
0