結果

問題 No.2927 Reverse Polish Equation
ユーザー 👑 loop0919
提出日時 2024-09-25 09:57:08
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 163 ms / 2,000 ms
コード長 1,321 bytes
コンパイル時間 14,581 ms
コンパイル使用メモリ 402,864 KB
実行使用メモリ 14,336 KB
最終ジャッジ日時 2024-10-16 00:21:13
合計ジャッジ時間 18,749 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::cmp::{min, max};
use proconio::input;

fn rev_polish(s: &Vec<String>, x: i64) -> i64 {
    let mut stack = Vec::<i64>::new();

    for token in s.iter() {
        match token.as_str() {
            "+" => {
                let a = stack.pop().unwrap();
                let b = stack.pop().unwrap();
                stack.push(a + b);
            },
            "min" => {
                let a = stack.pop().unwrap();
                let b = stack.pop().unwrap();
                stack.push(min(a, b));
            },
            "max" => {
                let a = stack.pop().unwrap();
                let b = stack.pop().unwrap();
                stack.push(max(a, b));
            },
            "X" => {
                stack.push(x);
            },
            _ => {
                stack.push(token.parse::<i64>().unwrap());
            }
        }
    }
    stack.pop().unwrap()
}

fn main() {
    input! {
        n: usize,
        y: i64,
        s: [String; n],
    }

    let mut ok: i64 = y;
    let mut ng: i64 = -1;

    while ok - ng > 1 {
        let mid = (ok + ng) / 2;
        if rev_polish(&s, mid) >=  y {
            ok = mid;
        } else {
            ng = mid;
        }
    }

    if rev_polish(&s, ok) == y {
        println!("{}", ok);
    } else {
        println!("-1");
    }
}
0