結果

問題 No.1470 Mex Sum
ユーザー mai
提出日時 2021-04-20 00:35:31
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 26 ms / 2,000 ms
コード長 1,927 bytes
コンパイル時間 12,380 ms
コンパイル使用メモリ 393,304 KB
実行使用メモリ 13,752 KB
最終ジャッジ日時 2024-07-04 05:13:15
合計ジャッジ時間 14,742 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 49
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::collections::*;

#[allow(unused)]
struct Scanner<R: std::io::BufRead> {
    stack: VecDeque<String>,
    input: R,
}
impl<R: std::io::BufRead> Scanner<R> {
    fn new(input: R) -> Self {
        Self {
            stack: VecDeque::<String>::new(),
            input: input,
        }
    }
    fn take(&mut self) -> Option<String> {
        if let Some(s) = self.stack.pop_front() {
            return Some(s);
        }
        self.enqueue_line();
        self.stack.pop_front()
    }
    fn enqueue(&mut self, t: String) {
        t.trim()
            .split_ascii_whitespace()
            .for_each(|s| self.stack.push_back(s.to_owned()));
    }
    fn enqueue_line(&mut self) {
        let mut t = String::new();
        self.input.read_line(&mut t).ok();
        self.enqueue(t);
    }
    fn enqueue_to_end(&mut self) {
        let mut t = String::new();
        self.input.read_to_string(&mut t).ok();
        self.enqueue(t);
    }
}

macro_rules! scan {
    ($io:expr => $t:ty) => {
        $io.take().unwrap().parse::<$t>().unwrap()
    };
    ($io:expr => $t:tt * $n:expr) => ((0..$n).map(|_| scan!($io => $t)).collect::<Vec<_>>());
    ($io:expr => $($t:tt),*) => (($(scan!($io => $t)),*));
}

fn main() {
    solve(Scanner::<std::io::StdinLock>::new(std::io::stdin().lock()))
}

fn solve<R: std::io::BufRead>(mut cin: Scanner<R>) {
    cin.enqueue_to_end();
    let n = scan!(cin => i32);
    let aa = scan!(cin => i32 * n);
    let mut one = 0 as i64;
    let mut two = 0 as i64;
    let mut three = 0 as i64;
    for a in aa {
        match a {
            1 => one += 1,
            2 => two += 1,
            _ => three += 1,
        }
    }
    let mut total = 0;
    total += 2 * one * (one - 1) / 2;
    total += 1 * two * (two - 1) / 2;
    total += 1 * three * (three - 1) / 2;
    total += 3 * one * two;
    total += 2 * one * three;
    total += 1 * two * three;
    println!("{}", total);
}
0