結果

問題 No.1115 二つの数列 / Two Sequences
ユーザー koba-e964koba-e964
提出日時 2021-10-06 16:56:54
言語 Rust
(1.77.0)
結果
AC  
実行時間 48 ms / 2,000 ms
コード長 2,210 bytes
コンパイル時間 1,604 ms
コンパイル使用メモリ 144,512 KB
実行使用メモリ 6,600 KB
最終ジャッジ日時 2023-09-30 08:47:56
合計ジャッジ時間 4,827 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 41 ms
5,812 KB
testcase_04 AC 47 ms
6,548 KB
testcase_05 AC 41 ms
5,800 KB
testcase_06 AC 37 ms
5,488 KB
testcase_07 AC 47 ms
6,520 KB
testcase_08 AC 3 ms
4,376 KB
testcase_09 AC 21 ms
4,552 KB
testcase_10 AC 37 ms
6,600 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 48 ms
6,596 KB
testcase_13 AC 47 ms
6,516 KB
testcase_14 AC 48 ms
6,596 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 5 ms
4,376 KB
testcase_24 AC 17 ms
4,380 KB
testcase_25 AC 35 ms
5,420 KB
testcase_26 AC 9 ms
4,380 KB
testcase_27 AC 20 ms
4,380 KB
testcase_28 AC 28 ms
4,588 KB
testcase_29 AC 38 ms
5,560 KB
testcase_30 AC 45 ms
6,408 KB
testcase_31 AC 12 ms
4,376 KB
testcase_32 AC 8 ms
4,376 KB
testcase_33 AC 32 ms
5,844 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 1 ms
4,380 KB
testcase_37 AC 1 ms
4,376 KB
testcase_38 AC 1 ms
4,376 KB
testcase_39 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes.by_ref().map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr,) => {};
    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };
    ($next:expr, usize1) => (read_value!($next, usize) - 1);
    ($next:expr, $t:ty) => ($next().parse::<$t>().expect("Parse error"));
}

fn inversion_by_merge(a: &mut [usize]) -> i64 {
    if a.len() <= 1 {
        return 0;
    }
    let mut ans = 0;
    let mut tmp = Vec::with_capacity(a.len());
    {
        let mid = a.len() / 2;
        let (x, y) = a.split_at_mut(mid);
        ans += inversion_by_merge(x);
        ans += inversion_by_merge(y);
        let mut p0 = 0;
        let mut p1 = 0;
        while p0 < x.len() && p1 < y.len() {
            if x[p0] <= y[p1] {
                tmp.push(x[p0]);
                p0 += 1;
            } else {
                tmp.push(y[p1]);
                p1 += 1;
                ans += (x.len() - p0) as i64;
            }
        }
        while p0 < x.len() {
            tmp.push(x[p0]);
            p0 += 1;
        }
        while p1 < y.len() {
            tmp.push(y[p1]);
            p1 += 1;
        }
    }
    for i in 0..a.len() {
        a[i] = tmp[i];
    }
    ans
}

fn main() {
    input! {
        n: usize,
        a: [usize1; n],
        b: [usize1; n],
    }
    let mut inva = vec![0; n];
    for i in 0..n {
        inva[a[i]] = i;
    }
    let mut p = vec![0; n];
    for i in 0..n {
        p[i] = inva[b[i]];
    }
    println!("{}", inversion_by_merge(&mut p));
}
0