結果

問題 No.1268 Fruit Rush 2
ユーザー StrorkisStrorkis
提出日時 2020-10-24 14:14:29
言語 Rust
(1.77.0 + proconio)
結果
AC  
実行時間 67 ms / 2,000 ms
コード長 3,514 bytes
コンパイル時間 15,696 ms
コンパイル使用メモリ 379,172 KB
実行使用メモリ 8,896 KB
最終ジャッジ日時 2024-07-21 15:14:33
合計ジャッジ時間 16,790 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 43 ms
7,132 KB
testcase_17 AC 37 ms
6,412 KB
testcase_18 AC 40 ms
6,656 KB
testcase_19 AC 40 ms
6,644 KB
testcase_20 AC 39 ms
6,732 KB
testcase_21 AC 38 ms
6,436 KB
testcase_22 AC 43 ms
7,144 KB
testcase_23 AC 43 ms
7,148 KB
testcase_24 AC 39 ms
6,636 KB
testcase_25 AC 39 ms
6,580 KB
testcase_26 AC 53 ms
8,892 KB
testcase_27 AC 54 ms
8,888 KB
testcase_28 AC 54 ms
8,892 KB
testcase_29 AC 54 ms
8,896 KB
testcase_30 AC 54 ms
8,764 KB
testcase_31 AC 54 ms
8,764 KB
testcase_32 AC 55 ms
8,640 KB
testcase_33 AC 48 ms
8,892 KB
testcase_34 AC 51 ms
8,764 KB
testcase_35 AC 67 ms
8,764 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: the item `FromIterator` is imported redundantly
   --> src/main.rs:103:13
    |
103 |         use std::iter::FromIterator;
    |             ^^^^^^^^^^^^^^^^^^^^^^^
   --> /tmp/rust-20240322-9800-yefgu1/rustc-1.77.0-src/library/std/src/prelude/mod.rs:129:13
    |
    = note: the item `FromIterator` is already defined here
    |
    = note: `#[warn(unused_imports)]` on by default

ソースコード

diff #

use std::io::{self, BufRead, Write};
use std::str::FromStr;
use std::fmt;

struct Io1<T>(T);

impl<T: FromStr> FromStr for Io1<T> {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Io1(s.trim_end().parse().ok().unwrap()))
    }
}

struct Io2<T1, T2>(T1, T2);

impl<T1: FromStr, T2: FromStr> FromStr for Io2<T1, T2> {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut iter = s.split_whitespace();
        Ok(Io2(
            iter.next().unwrap().parse().ok().unwrap(),
            iter.next().unwrap().parse().ok().unwrap(),
        ))
    }
}

struct Io3<T1, T2, T3>(T1, T2, T3);

impl<T1, T2, T3> FromStr for Io3<T1, T2, T3>
where
    T1: FromStr, T2: FromStr, T3: FromStr
{
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut iter = s.split_whitespace();
        Ok(Io3(
            iter.next().unwrap().parse().ok().unwrap(),
            iter.next().unwrap().parse().ok().unwrap(),
            iter.next().unwrap().parse().ok().unwrap(),
        ))
    }
}

struct Ios(Vec<char>);

impl std::str::FromStr for Ios {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Ok(Ios(s.chars().collect()))
    }
}

impl fmt::Display for Ios {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0.iter().collect::<String>())
    }
}

struct Iov<T>(Vec<T>);

impl<T: FromStr> FromStr for Iov<T> {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let iter = s.split_whitespace();
        Ok(Iov(iter.map(|x| x.parse().ok().unwrap()).collect()))
    }
}

impl<T: fmt::Display> fmt::Display for Iov<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}",
            self.0.iter().map(|x| x.to_string())
                .collect::<Vec<_>>().join(" ")
        )
    }
}

struct Solver<'a> {
    reader: io::BufReader<io::StdinLock<'a>>,
    writer: io::BufWriter<io::StdoutLock<'a>>,
}

impl Solver<'_> {
    fn read<T: FromStr>(&mut self) -> T {
        let mut input = String::new();
        self.reader.read_line(&mut input).unwrap();
        input.parse().ok().unwrap()
    }

    fn writeln<T: fmt::Display>(&mut self, ans: T) {
        writeln!(self.writer, "{}", ans).unwrap();
    }

    // 1 2 3 5 8 13 21 34 55 89 144

    fn solve(&mut self) {
        use std::collections::BTreeMap;
        use std::iter::FromIterator;

        let Io1(n): Io1<usize> = self.read();
        let Iov(a): Iov<i64> = self.read();
        let mut ans: i64 = n as i64;
        let mut prev = 0;
        let mut map = BTreeMap::from_iter(a.into_iter().map(|x| (x, 1)));
        while map.len() > 1 {
            let (&k1, &v1) = map.iter().next().unwrap();
            map.remove(&k1);
            let (&k2, &v2) = map.iter().next().unwrap();
            if prev > 0 {
                *map.entry(k1 + 1).or_insert(0) += prev;
            }
            if k2 - k1 == 1 {
                let v = v1 * v2;
                ans += v;
                prev = v;
            } else {
                prev = 0;
            }
        }
        self.writeln(ans);
    }

    fn run() {
        let (stdin, stdout) = (io::stdin(), io::stdout());
        let reader = io::BufReader::new(stdin.lock());
        let writer = io::BufWriter::new(stdout.lock());
        let mut solver = Solver { reader, writer };
        solver.solve();
    }
}

fn main() {
    Solver::run();
}
0