結果
| 問題 | No.366 ロボットソート | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-03-24 16:11:11 | 
| 言語 | Rust (1.83.0 + proconio) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1 ms / 2,000 ms | 
| コード長 | 2,667 bytes | 
| コンパイル時間 | 13,868 ms | 
| コンパイル使用メモリ | 393,280 KB | 
| 実行使用メモリ | 6,824 KB | 
| 最終ジャッジ日時 | 2024-12-31 15:10:15 | 
| 合計ジャッジ時間 | 15,058 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 23 | 
ソースコード
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");
    }
}
            
            
            
        