#![allow(dead_code, unused_imports, unused_macros, unused_mut, unused_variables)] use std::cmp::{max, min, Ordering, Reverse}; use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque}; use std::fs::File; use std::io::{self, BufWriter, Read, Write}; // ----- TYPES ----- type Ll = i64; type Pii = (i32, i32); // ----- FILE IO ----- // stdio("queue-with-stack") picks the mode from the directory it runs in: // name.in exists -> read name.in, write name.out (USACO + `usaco run/test`) // name.in missing -> read stdin, write stdout (Codeforces submission) // So the same file works locally and when pasted into a judge. fn stdio(name: &str) -> (Scanner, Box) { let infile = format!("{name}.in"); if std::path::Path::new(&infile).exists() { let src = std::fs::read_to_string(&infile).expect("could not read .in file"); let out = File::create(format!("{name}.out")).expect("could not create .out file"); (Scanner::new(&src), Box::new(BufWriter::new(out))) } else { let mut src = String::new(); io::stdin() .read_to_string(&mut src) .expect("could not read stdin"); (Scanner::new(&src), Box::new(BufWriter::new(io::stdout()))) } } // ----- INPUT ----- // Slurps the whole input, then hands out whitespace-separated tokens. // let n: usize = sc.next(); one token // let (a, b) = (sc.next::(), sc.next::()); // let v: Vec = sc.vec(n); n tokens // let s: Vec = sc.bytes(); a word as bytes (s[i] - b'a') struct Scanner { toks: std::str::SplitAsciiWhitespace<'static>, } impl Scanner { fn new(src: &str) -> Scanner { let leaked: &'static str = Box::leak(src.to_owned().into_boxed_str()); Scanner { toks: leaked.split_ascii_whitespace(), } } fn next(&mut self) -> T { let tok = self.toks.next().expect("ran out of input"); tok.parse().ok().expect("could not parse token") } fn vec(&mut self, n: usize) -> Vec { (0..n).map(|_| self.next()).collect() } fn pair(&mut self) -> (T, U) { (self.next(), self.next()) } fn pairs(&mut self, n: usize) -> Vec<(T, U)> { (0..n).map(|_| self.pair()).collect() } fn bytes(&mut self) -> Vec { self.next::().into_bytes() } fn chars(&mut self) -> Vec { self.next::().chars().collect() } } // ----- OUTPUT ----- // Write with writeln!(out, "{}", x). These help with the common shapes: fn yesno(b: bool) -> &'static str { if b { "YES" } else { "NO" } } // join(&v, " ") -> "1 2 3" fn join(v: &[T], sep: &str) -> String { v.iter() .map(|x| x.to_string()) .collect::>() .join(sep) } // ----- DEBUG ----- // dbg!(x) is built in and goes to stderr, so it never pollutes the .out file. // ----- MAX MIN ----- fn remax(a: &mut T, b: T) -> bool { if b > *a { *a = b; true } else { false } } fn remin(a: &mut T, b: T) -> bool { if b < *a { *a = b; true } else { false } } struct MyQueue { in_stk: VecDeque, out_stk: VecDeque, } impl MyQueue { fn new() -> Self { Self { in_stk: VecDeque::new(), out_stk: VecDeque::new(), } } fn push(&mut self, x: i32) { self.in_stk.push_back(x); } fn pop(&mut self) -> i32 { let val = self.peek(); self.out_stk.pop_back(); val } fn peek(&mut self) -> i32 { if self.out_stk.is_empty() { while let Some(x) = self.in_stk.pop_back() { self.out_stk.push_back(x); } } self.out_stk.back().copied().unwrap_or(0) } fn empty(&self) -> bool { self.in_stk.is_empty() && self.out_stk.is_empty() } } fn main() { let (mut sc, mut out) = stdio("queue-with-stack"); let t: usize = sc.next(); let mut queue: MyQueue = MyQueue::new(); for _ in 0..t { let command: String = sc.next(); match command.as_str() { "MyQueue" => { queue = MyQueue::new(); } "push" => { let x = sc.next(); queue.push(x); } "peek" => { writeln!(out, "{}", queue.peek()).unwrap(); } "pop" => { writeln!(out, "{}", queue.pop()).unwrap(); } "empty" => { writeln!(out, "{}", queue.empty()).unwrap(); } _ => { writeln!(out, "?").unwrap(); } } } }