Skip to content

Instantly share code, notes, and snippets.

@thinkofher
Created April 5, 2021 22:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinkofher/ac7267559e3c75fc98e3cf2343325050 to your computer and use it in GitHub Desktop.
Save thinkofher/ac7267559e3c75fc98e3cf2343325050 to your computer and use it in GitHub Desktop.
rust konkurs 2
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