結果

問題 No.2927 Reverse Polish Equation
ユーザー tnodino
提出日時 2024-10-05 16:34:24
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 403 ms / 2,000 ms
コード長 2,244 bytes
コンパイル時間 15,957 ms
コンパイル使用メモリ 379,064 KB
実行使用メモリ 14,208 KB
最終ジャッジ日時 2024-10-16 00:22:03
合計ジャッジ時間 23,896 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 43
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#[fastout]
#[allow(non_snake_case)]
fn main() {
    input! {
        (Q, Y): (usize, isize),
        s: [String; Q],
    }
    assert!(1 <= Q && Q <= 200_000);
    assert!(0 <= Y && Y <= 100_000_000_000_000);
    for i in 0..Q {
        match s[i].parse::<isize>() {
            Ok(x) => {assert!(0 <= x && x <= 1_000_000_000)},
            Err(_) => {assert!(["X", "+", "min", "max"].contains(&s[i].as_str()))},
        };
    }
    let mut ng = -1;
    let mut ok = Y;
    for _ in 0..100 {
        let mid = (ok + ng) / 2;
        let mut stc = Vec::new();
        for i in 0..Q {
            if s[i] == "X" {
                stc.push(mid);
            }
            else if s[i] == "+" {
                let x = stc.pop().unwrap();
                let y = stc.pop().unwrap();
                stc.push(x + y);
            }
            else if s[i] == "min" {
                let x = stc.pop().unwrap();
                let y = stc.pop().unwrap();
                stc.push(min(x, y));
            }
            else if s[i] == "max" {
                let x = stc.pop().unwrap();
                let y = stc.pop().unwrap();
                stc.push(max(x, y));
            }
            else {
                stc.push(s[i].parse::<isize>().unwrap());
            }
        }
        assert!(stc.len() == 1);
        if stc[0] >= Y {
            ok = mid;
        }
        else {
            ng = mid;
        }
    }
    let mut stc = Vec::new();
    for i in 0..Q {
        if s[i] == "X" {
            stc.push(ok);
        }
        else if s[i] == "+" {
            let x = stc.pop().unwrap();
            let y = stc.pop().unwrap();
            stc.push(x + y);
        }
        else if s[i] == "min" {
            let x = stc.pop().unwrap();
            let y = stc.pop().unwrap();
            stc.push(min(x, y));
        }
        else if s[i] == "max" {
            let x = stc.pop().unwrap();
            let y = stc.pop().unwrap();
            stc.push(max(x, y));
        }
        else {
            stc.push(s[i].parse::<isize>().unwrap());
        }
    }
    if stc[0] != Y {
        println!("-1");
    }
    else {
        println!("{}", ok);
    }
}
0