結果

問題 No.365 ジェンガソート
ユーザー hatoohatoo
提出日時 2017-11-13 16:06:26
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 4,156 bytes
コンパイル時間 1,272 ms
コンパイル使用メモリ 153,040 KB
実行使用メモリ 5,132 KB
最終ジャッジ日時 2023-08-16 11:59:55
合計ジャッジ時間 3,697 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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,376 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 1 ms
4,384 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 4 ms
4,380 KB
testcase_19 AC 40 ms
4,864 KB
testcase_20 AC 12 ms
4,504 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 AC 36 ms
5,116 KB
testcase_37 AC 37 ms
5,120 KB
testcase_38 WA -
testcase_39 AC 36 ms
5,064 KB
testcase_40 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: associated function `add` is never used
   --> Main.rs:111:8
    |
111 |     fn add(&mut self, k: usize, a: &T) {
    |        ^^^
    |
    = note: `#[warn(dead_code)]` on by default

warning: 1 warning emitted

ソースコード

diff #

#[allow(unused_imports)]
use std::cmp::{max, min, Ordering};
#[allow(unused_imports)]
use std::collections::{HashMap, HashSet, BinaryHeap, VecDeque};
#[allow(unused_imports)]
use std::iter::FromIterator;
#[allow(unused_imports)]
use std::io::stdin;

mod util {
    use std::io::stdin;
    use std::str::FromStr;
    use std::fmt::Debug;

    #[allow(dead_code)]
    pub fn line() -> String {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().to_string()
    }

    #[allow(dead_code)]
    pub fn gets<T: FromStr>() -> Vec<T>
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.split_whitespace()
            .map(|t| t.parse().unwrap())
            .collect()
    }
}

#[allow(unused_macros)]
macro_rules! get {
    ($t:ty) => {
        {
            let mut line: String = String::new();
            stdin().read_line(&mut line).unwrap();
            line.trim().parse::<$t>().unwrap()
        }
    };
    ($($t:ty),*) => {
        {
            let mut line: String = String::new();
            stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            (
                $(iter.next().unwrap().parse::<$t>().unwrap(),)*
            )
        }
    };
    ($t:ty; $n:expr) => {
        (0..$n).map(|_|
                    get!($t)
                   ).collect::<Vec<_>>()
    };
    ($($t:ty),*; $n:expr) => {
        (0..$n).map(|_|
                    get!($($t),*)
                   ).collect::<Vec<_>>()
    };
    ($t:ty ;;) => {
        {
            let mut line: String = String::new();
            stdin().read_line(&mut line).unwrap();
            line.split_whitespace()
                .map(|t| t.parse::<$t>().unwrap())
                .collect::<Vec<_>>()
        }
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

struct SEG<T: Clone, F: Fn(&T, &T) -> T> {
    n: usize,
    buf: Vec<T>,
    reducer: F,
}

impl<T: Clone, F: Fn(&T, &T) -> T> SEG<T, F> {
    fn new(n: usize, zero: &T, f: F) -> SEG<T, F> {
        let n = (1..)
            .map(|i| 2usize.pow(i as u32))
            .find(|&x| x > n)
            .unwrap();
        SEG {
            n: n,
            buf: vec![zero.clone(); 2 * n],
            reducer: f,
        }
    }

    fn update(&mut self, k: usize, a: T) {
        let mut k = k + self.n - 1;
        self.buf[k] = a;

        while k > 0 {
            k = (k - 1) / 2;
            self.buf[k] = (self.reducer)(&self.buf[k * 2 + 1], &self.buf[k * 2 + 2]);
        }
    }

    fn add(&mut self, k: usize, a: &T) {
        let mut k = k + self.n - 1;
        self.buf[k] = (self.reducer)(&self.buf[k], a);

        while k > 0 {
            k = (k - 1) / 2;
            self.buf[k] = (self.reducer)(&self.buf[k * 2 + 1], &self.buf[k * 2 + 2]);
        }

    }

    fn q(&self, a: usize, b: usize, k: usize, l: usize, r: usize) -> Option<T> {
        if r <= a || b <= l {
            return None;
        }

        if a <= l && r <= b {
            Some(self.buf[k].clone())
        } else {
            let vl = self.q(a, b, k * 2 + 1, l, (l + r) / 2);
            let vr = self.q(a, b, k * 2 + 2, (l + r) / 2, r);

            match (vl, vr) {
                (Some(l), Some(r)) => Some((self.reducer)(&l, &r)),
                (Some(l), None) => Some(l),
                (None, Some(r)) => Some(r),
                _ => None,
            }
        }
    }

    fn query(&self, a: usize, b: usize) -> T {
        self.q(a, b, 0, 0, self.n).unwrap()
    }
}

fn main() {
    let n = get!(usize);
    let a = get!(usize;;);

    let mut index = vec![0; n];

    for (i, &x) in a.iter().enumerate() {
        index[x - 1] = i;
    }

    let mut seg = SEG::new(n + 1, &0, |&a, &b| a + b);
    let mut ans = 0;
    for (k, &i) in index.iter().rev().enumerate() {
        if seg.query(i, n) != k {
            ans += 1;
        }
        seg.update(i, 1);
    }

    println!("{}", ans);
}
0