結果

問題 No.2759 Take Pictures, Elements?
ユーザー Yukino DX.
提出日時 2024-05-17 22:00:02
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 28 ms / 2,000 ms
コード長 895 bytes
コンパイル時間 16,735 ms
コンパイル使用メモリ 384,192 KB
実行使用メモリ 10,112 KB
最終ジャッジ日時 2025-06-20 13:17:57
合計ジャッジ時間 13,246 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::collections::{BTreeSet, HashMap};

use proconio::input;

fn main() {
    input! {
        n:usize,
        q:usize,
        a:[usize;n],
        b:[usize;q],
    }

    let mut poss = HashMap::new();
    for i in 0..n {
        poss.entry(a[i]).or_insert(BTreeSet::new()).insert(i);
    }

    const INF: usize = std::usize::MAX;
    let mut dp = vec![vec![INF; n]; q + 1];
    dp[0][0] = 0;
    for i in 0..q {
        for j in 0..n {
            if dp[i][j] == INF {
                continue;
            }

            if let Some(&r) = poss[&b[i]].range(j..).next() {
                dp[i + 1][r] = dp[i + 1][r].min(dp[i][j] + r - j);
            }

            if let Some(&l) = poss[&b[i]].range(..=j).last() {
                dp[i + 1][l] = dp[i + 1][l].min(dp[i][j] + j - l);
            }
        }
    }

    let ans = *dp[q].iter().min().unwrap();
    println!("{}", ans);
}
0