結果

問題 No.2927 Reverse Polish Equation
ユーザー yiwiy9
提出日時 2025-01-22 00:57:03
言語 Rust
(1.83.0 + proconio)
結果
WA  
実行時間 -
コード長 1,440 bytes
コンパイル時間 21,131 ms
コンパイル使用メモリ 377,448 KB
実行使用メモリ 14,236 KB
最終ジャッジ日時 2025-01-22 00:58:08
合計ジャッジ時間 28,673 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 41 WA * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

use proconio::input;

const INF: i64 = 1 << 60;

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

    let mut left = -1;
    let mut right = INF;
    while right - left > 1 {
        let mid = (left + right) / 2;

        let fx = calc_reverse_polish_notation(&s, mid);

        if fx >= y {
            right = mid;
        } else {
            left = mid;
        }
    }

    let fx = calc_reverse_polish_notation(&s, right);

    println!("{}", if fx == y { right } else { -1 });
}

fn calc_reverse_polish_notation(s: &[String], x: i64) -> i64 {
    let mut stack: Vec<i64> = vec![];
    for s_i in s {
        match s_i.as_str() {
            "max" => {
                let a = stack.pop().unwrap();
                let b = stack.pop().unwrap();
                stack.push(a.max(b));
            }
            "min" => {
                let a = stack.pop().unwrap();
                let b = stack.pop().unwrap();
                stack.push(a.min(b));
            }
            "+" => {
                let a = stack.pop().unwrap();
                let b = stack.pop().unwrap();
                stack.push(a + b);
            }
            "X" => {
                stack.push(x);
            }
            _ => {
                // Vec<char>だとparseが使えないのでStringにしてparseする
                stack.push(s_i.parse().unwrap());
            }
        }
    }
    stack[0]
}
0