結果

問題 No.2927 Reverse Polish Equation
ユーザー manuo
提出日時 2024-10-12 16:10:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 551 ms / 2,000 ms
コード長 1,077 bytes
コンパイル時間 219 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 130,732 KB
最終ジャッジ日時 2024-10-16 00:25:16
合計ジャッジ時間 12,955 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque, defaultdict, Counter
from bisect import bisect_left, bisect_right
from itertools import permutations, combinations
from functools import cmp_to_key, cache
from heapq import heappop, heappush
import math, sys
import pypyjit
pypyjit.set_param('max_unroll_recursion=-1')
sys.setrecursionlimit(10**7)
_int = lambda x: int(x)-1
MOD = 998244353
INF = 1<<62
Yes, No = "Yes", "No"

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

def calc(x):
    stack = []
    for c in S:
        if c == "+":
            l, r = stack.pop(), stack.pop()
            stack.append(l+r)
        elif c == "min":
            l, r = stack.pop(), stack.pop()
            stack.append(min(l, r))
        elif c == "max":
            l, r = stack.pop(), stack.pop()
            stack.append(max(l, r))
        elif c == "X":
            stack.append(x)
        else:
            stack.append(int(c))
    return stack[0]            
l, r = -1, 10**15
while r-l > 1:
    m = (l+r)//2
    res = calc(m)
    if res >= Y: r = m
    else: l = m
print(r if calc(r) == Y else -1)
0