結果
問題 |
No.2927 Reverse Polish Equation
|
ユーザー |
|
提出日時 | 2025-01-22 01:03:47 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 179 ms / 2,000 ms |
コード長 | 1,415 bytes |
コンパイル時間 | 22,818 ms |
コンパイル使用メモリ | 379,436 KB |
実行使用メモリ | 14,408 KB |
最終ジャッジ日時 | 2025-01-22 01:04:18 |
合計ジャッジ時間 | 29,739 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 43 |
ソースコード
use proconio::input; fn main() { input! { q: usize, y: i64, s: [String; q], } let mut left = -1; let mut right = y + 1; 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] }