結果

問題 No.2759 Take Pictures, Elements?
ユーザー naut3naut3
提出日時 2024-05-17 22:44:45
言語 Rust
(1.77.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 4,972 bytes
コンパイル時間 13,419 ms
コンパイル使用メモリ 401,744 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-17 22:45:00
合計ジャッジ時間 15,209 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#![allow(non_snake_case, unused_imports)]
use proconio::{fastout, input, marker::*};

#[fastout]
fn main() {
    input! {
        N: usize, Q: usize,
        A: [usize; N],
        B: [usize; Q],
    }

    let mut dp = vec![usize::MAX; N];
    dp[0] = 0;

    // margin
    // for i in 0..N {
    //     fromleft.insert(i, N - 1 - i);
    //     fromright.insert(i, i);
    // }

    let (z, zi) = coordinate_compression(&A);

    let mut pos = vec![vec![]; z.len()];

    for (i, a) in A.iter().enumerate() {
        let &p = zi.get(a).unwrap();
        pos[p].push(i);
    }

    for i in 0..Q {
        let &zb = zi.get(&B[i]).unwrap();
        let ps = &pos[zb];

        let mut fromleft = SegmentTree::new(N, |&x, &y| std::cmp::min(x, y), usize::MAX);
        let mut fromright = SegmentTree::new(N, |&x, &y| std::cmp::min(x, y), usize::MAX);

        if i == 0 {
            fromleft.insert(0, N - 1);
            fromright.insert(0, 0);
        } else {
            let &zbp = zi.get(&B[i - 1]).unwrap();
            let psp = &pos[zbp];

            for &p in psp {
                fromleft.insert(p, dp[p] + N - 1 - p);
                fromright.insert(p, dp[p] + p);
            }
        }

        dp = vec![usize::MAX; N];

        for &p in ps {
            let fl = fromleft.prod(..=p) - (N - 1 - p);
            let fr = fromright.prod(p..) - p;

            dp[p] = std::cmp::min(dp[p], fl);
            dp[p] = std::cmp::min(dp[p], fr);
        }
    }

    println!("{}", dp.iter().min().unwrap());
}

pub fn coordinate_compression<T: std::cmp::Ord + Clone + Copy>(
    values: &[T],
) -> (Vec<T>, std::collections::BTreeMap<T, usize>) {
    let s: std::collections::BTreeSet<T> = values.iter().cloned().collect();
    let z: Vec<T> = s.iter().cloned().collect();
    let zinv: std::collections::BTreeMap<T, usize> =
        z.iter().enumerate().map(|(i, &v)| (v, i)).collect();
    (z, zinv)
}

pub struct SegmentTree<M> {
    size: usize,
    tree: Vec<M>,
    op: fn(&M, &M) -> M,
    id: M,
}

impl<M: Copy + PartialEq> SegmentTree<M> {
    /// self = [id; size], self.op = op, self.id = id
    pub fn new(size: usize, op: fn(&M, &M) -> M, id: M) -> Self {
        return Self {
            size: size,
            tree: vec![id; 2 * size],
            op: op,
            id: id,
        };
    }

    /// self = arr, self.op = op, self.id = id
    pub fn from(arr: &[M], op: fn(&M, &M) -> M, id: M) -> Self {
        let size = arr.len();
        let mut tree = vec![id; 2 * size];

        for i in 0..size {
            tree[i + size] = arr[i];

            assert!(op(&id, &arr[i]) == arr[i]);
        }

        for i in (1..size).rev() {
            tree[i] = op(&tree[i << 1], &tree[i << 1 | 1]);
        }

        return Self {
            size: size,
            tree: tree,
            op: op,
            id: id,
        };
    }

    /// self[pos] <- value
    pub fn insert(&mut self, mut pos: usize, value: M) -> () {
        pos += self.size;
        self.tree[pos] = value;

        while pos > 1 {
            pos >>= 1;
            self.tree[pos] = (self.op)(&self.tree[pos << 1], &self.tree[pos << 1 | 1]);
        }
    }

    /// return self[pos]
    pub fn get_point(&self, pos: usize) -> M {
        return self[pos];
    }

    /// return Π_{i ∈ [left, right)} self[i]
    pub fn get(&self, left: usize, right: usize) -> M {
        let (mut l, mut r) = (left + self.size, right + self.size);
        let (mut vl, mut vr) = (self.id, self.id);

        while l < r {
            if l & 1 == 1 {
                vl = (self.op)(&vl, &self.tree[l]);
                l += 1;
            }

            if r & 1 == 1 {
                r -= 1;
                vr = (self.op)(&self.tree[r], &vr);
            }

            l >>= 1;
            r >>= 1;
        }

        return (self.op)(&vl, &vr);
    }

    /// return Π_{i ∈ range} self[i]
    pub fn prod<R: std::ops::RangeBounds<usize>>(&self, range: R) -> M {
        let left = match range.start_bound() {
            std::ops::Bound::Included(&l) => l,
            std::ops::Bound::Excluded(&l) => l + 1,
            std::ops::Bound::Unbounded => 0,
        };

        let right = match range.end_bound() {
            std::ops::Bound::Included(&r) => r + 1,
            std::ops::Bound::Excluded(&r) => r,
            std::ops::Bound::Unbounded => self.size,
        };

        return self.get(left, right);
    }
}

impl<M> std::ops::Index<usize> for SegmentTree<M> {
    type Output = M;
    fn index(&self, index: usize) -> &Self::Output {
        &self.tree[index + self.size]
    }
}

impl<M: std::fmt::Display> std::fmt::Display for SegmentTree<M> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            self.tree[self.size..]
                .iter()
                .map(|x| x.to_string())
                .collect::<Vec<_>>()
                .join(" ")
        )
    }
}
0