結果

問題 No.366 ロボットソート
ユーザー tonyu0tonyu0
提出日時 2020-03-24 16:11:11
言語 Rust
(1.77.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,667 bytes
コンパイル時間 1,445 ms
コンパイル使用メモリ 167,376 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-30 05:34:14
合計ジャッジ時間 2,822 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,384 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::*;

mod fenwick_tree {
    use std::ops::*;
    pub struct FenwickTree<T, F> {
        data: Vec<T>,
        identity: T,
        operation: F,
    }
    impl<T: Copy + Clone, F: Fn(T, T) -> T> FenwickTree<T, F> {
        pub fn new(size: usize, id: T, op: F) -> FenwickTree<T, F> {
            FenwickTree {
                data: vec![id; size + 1],
                identity: id,
                operation: op,
            }
        }
        pub fn query(&self, i: usize) -> T {
            let mut res = self.identity;
            let mut idx = i as isize - 1;
            while idx >= 0 {
                res = (self.operation)(res, self.data[idx as usize]);
                idx = (idx & (idx + 1)) - 1;
            }
            res
        }
        pub fn update(&mut self, i: usize, x: T) {
            let mut idx = i;
            while idx < self.data.len() {
                self.data[idx] = (self.operation)(self.data[idx], x);
                idx |= idx + 1;
            }
        }
    }
}

fn main() {
    let mut s: String = String::new();
    std::io::stdin().read_to_string(&mut s).ok();
    let mut itr = s.trim().split_whitespace();
    let n: usize = itr.next().unwrap().parse().unwrap();
    let k: usize = itr.next().unwrap().parse().unwrap();
    let a: Vec<usize> = (0..n)
        .map(|_| itr.next().unwrap().parse().unwrap())
        .collect();

    let mut b = a.clone();
    b.sort();
    b.dedup();
    let mut c = std::collections::HashMap::new();
    for i in 0..b.len() {
        c.insert(b[i], i);
    }

    let mut set_num = 0;
    let mut set: Vec<Vec<usize>> = Vec::new();
    let mut checked = vec![false; n];
    for i in 0..n {
        if !checked[i] {
            set.push(Vec::new());
            let mut j = i;
            while j < n {
                checked[j] = true;
                set[set_num].push(a[j]);
                j += k;
            }
            set_num += 1;
        }
    }

    let mut ans = 0;
    for i in 0..set.len() {
        let mut bit = fenwick_tree::FenwickTree::new(1010, 0, |a, b| a + b);
        for j in 0..set[i].len() {
            ans += j - bit.query(c[&set[i][j]]);
            bit.update(c[&set[i][j]], 1);
        }
        set[i].sort();
    }

    let mut ok = true;
    for i in 0..set[0].len() {
        for j in 0..set.len() - 1 {
            if set[j + 1].len() <= i {
                break;
            }
            if set[j][i] > set[j + 1][i] {
                ok = false;
                break;
            }
        }
        if !ok {
            break;
        }
    }

    if ok {
        println!("{}", ans);
    } else {
        println!("-1");
    }
}
0