Files
CodeTest/baekjoon/나이순_정렬/solution_20388470.rs
2024-08-29 16:22:10 +09:00

67 lines
1.4 KiB
Rust

use std::io::{self, Write, BufWriter};
use std::cmp::Ordering;
macro_rules! parse_line {
($($t: ty),+) => ({
let mut line = String::new();
std::io::stdin().read_line(&mut line).unwrap();
let mut iter = line.split_whitespace();
($(iter.next().unwrap().parse::<$t>().unwrap()),+)
})
}
struct Client{
idx : usize,
age : usize,
name : String,
}
impl Client{
fn new(idx: usize, age: usize, name: String) -> Client{
Client{
idx, age, name,
}
}
}
impl PartialOrd for Client {
fn partial_cmp(&self, other: &Self) -> Option<Ordering>{
Some(self.age.partial_cmp(&other.age).unwrap()
.then(self.idx.partial_cmp(&other.idx).unwrap()))
}
}
impl Ord for Client{
fn cmp(&self, other: &Self) -> Ordering{
self.partial_cmp(other).unwrap()
}
}
impl PartialEq for Client{
fn eq(&self, other: &Self) -> bool{
(self.age == other.age) && (self.idx == other.idx)
}
}
impl Eq for Client{}
fn main(){
let n : usize = parse_line!(usize);
let mut vec : Vec<Client> = Vec::with_capacity(n);
for i in 0..n{
let (age, name) : (usize, String) = parse_line!(usize, String);
vec.push(Client::new(i, age, name));
}
let stdout = io::stdout();
let lock = stdout.lock();
let mut buff = BufWriter::new(lock);
vec.sort_unstable();
for client in vec.iter(){
buff.write_fmt(format_args!("{} {}\n", client.age, client.name)).expect("Failed to write");
}
}