結果

問題 No.2927 Reverse Polish Equation
コンテスト
ユーザー 寝癖
提出日時 2024-10-12 16:18:51
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 637 ms / 2,000 ms
コード長 748 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 337 ms
コンパイル使用メモリ 85,272 KB
実行使用メモリ 140,420 KB
最終ジャッジ日時 2026-05-05 13:01:15
合計ジャッジ時間 11,298 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

def f(x):
    A = []
    for s in S:
        if s.isdigit():
            A.append(int(s))
        elif s == 'X':
            A.append(x)
        elif s == '+':
            A[-2] += A[-1]
            A.pop()
        elif s == 'min':
            A[-2] = min(A[-2], A[-1])
            A.pop()
        else:
            A[-2] = max(A[-2], A[-1])
            A.pop()
    return A[0]

inf = 10**20
if f(inf) < Y:
    print(-1)
    exit()

if f(0) >= Y:
    if f(0) == Y:
        print(0)
    else:
        print(-1)
    exit()

ng, ok = 0, inf
while abs(ok-ng) > 1:
    mid = (ok+ng)//2
    if f(mid) >= Y:
        ok = mid
    else:
        ng = mid

if f(ok) == Y:
    print(ok)
else:
    print(-1)
0