結果
| 問題 | No.2927 Reverse Polish  Equation | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2024-10-12 17:19:42 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 667 ms / 2,000 ms | 
| コード長 | 862 bytes | 
| コンパイル時間 | 288 ms | 
| コンパイル使用メモリ | 82,372 KB | 
| 実行使用メモリ | 129,544 KB | 
| 最終ジャッジ日時 | 2024-10-16 00:30:29 | 
| 合計ジャッジ時間 | 14,792 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 43 | 
ソースコード
Q, Y = map(int, input().split())
S = list(map(str, input().split()))
def solve(S, x):
  stack = []
  for i in S:
    if i == "X" or (i != "max" and i != "min" and i != "+"):
      if i == "X":
        stack.append(x)
      else:
        stack.append(i)
    else:
      p = stack.pop()
      q = stack.pop()
      if i == "+":
        stack.append(int(p) + int(q))
      else:
        if i == "max":
          stack.append(max(int(p), int(q)))
        else:
          stack.append(min(int(p), int(q)))
  return stack[-1]
if "X" not in S:
  if int(solve(S, 0)) == Y:
    print(0)
  else:
    print(-1)
  exit()
left = -1
right = 10 ** 14
while left + 1 < right:
  mid = (left + right) // 2
  res = int(solve(S, mid))
  if res == Y:
    right = mid
  elif res < Y:
    left = mid
  else:
    right = mid
if solve(S, right) == Y:
  print(right)
else:
  print(-1)
            
            
            
        