rust konkurs 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::collections::HashSet; | |
use std::io::{self, BufRead}; | |
enum Token { | |
Query(i64), | |
Insert(i64), | |
} | |
impl Token { | |
fn parse(s: String) -> Option<Token> { | |
match s.split(" ").collect::<Vec<&str>>().as_slice() { | |
["insert", n] => match n.parse() { | |
Ok(n) => Some(Token::Insert(n)), | |
_ => None, | |
}, | |
["query", n] => match n.parse() { | |
Ok(n) => Some(Token::Query(n)), | |
_ => None, | |
}, | |
_ => None, | |
} | |
} | |
} | |
fn solve(tokens: Vec<Token>) { | |
let mut set = HashSet::new(); | |
for t in tokens { | |
match t { | |
Token::Insert(n) => { | |
set.insert(n); | |
} | |
Token::Query(n) => { | |
let answer = set.iter().filter(|e| e.to_owned() < &n).count(); | |
println!("{}->{}", n, answer); | |
} | |
} | |
} | |
} | |
fn main() { | |
let stdin = io::stdin(); | |
let lines: Vec<_> = stdin | |
.lock() | |
.lines() | |
.map(|s| s.unwrap_or("".to_string())) | |
.collect(); | |
let data: Vec<_> = lines | |
.into_iter() | |
.map(Token::parse) | |
.filter(|e| e.is_some()) | |
.map(|e| e.unwrap()) | |
.collect(); | |
solve(data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment