結果

問題 No.1268 Fruit Rush 2
ユーザー StrorkisStrorkis
提出日時 2020-10-24 14:14:29
言語 Rust
(1.72.1)
結果
AC  
実行時間 64 ms / 2,000 ms
コード長 3,514 bytes
コンパイル時間 2,360 ms
コンパイル使用メモリ 165,084 KB
実行使用メモリ 8,996 KB
最終ジャッジ日時 2023-09-28 20:25:31
合計ジャッジ時間 5,212 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 42 ms
7,108 KB
testcase_17 AC 37 ms
6,484 KB
testcase_18 AC 39 ms
6,628 KB
testcase_19 AC 38 ms
6,584 KB
testcase_20 AC 38 ms
6,588 KB
testcase_21 AC 37 ms
6,516 KB
testcase_22 AC 43 ms
7,064 KB
testcase_23 AC 42 ms
7,092 KB
testcase_24 AC 39 ms
6,608 KB
testcase_25 AC 38 ms
6,560 KB
testcase_26 AC 51 ms
8,952 KB
testcase_27 AC 51 ms
8,928 KB
testcase_28 AC 51 ms
8,996 KB
testcase_29 AC 51 ms
8,996 KB
testcase_30 AC 51 ms
8,924 KB
testcase_31 AC 51 ms
8,848 KB
testcase_32 AC 51 ms
8,840 KB
testcase_33 AC 47 ms
8,924 KB
testcase_34 AC 50 ms
8,840 KB
testcase_35 AC 64 ms
8,956 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: the item `FromIterator` is imported redundantly
   --> Main.rs:103:13
    |
103 |         use std::iter::FromIterator;
    |             ^^^^^^^^^^^^^^^^^^^^^^^
   --> /rustc/8460ca823e8367a30dda430efda790588b8c84d3/library/std/src/prelude/mod.rs:133:13
    |
    = note: the item `FromIterator` is already defined here
    |
    = note: `#[warn(unused_imports)]` on by default

warning: 1 warning emitted

ソースコード

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