結果

問題 No.2759 Take Pictures, Elements?
ユーザー Yukino DX.Yukino DX.
提出日時 2024-05-17 22:00:02
言語 Rust
(1.77.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 895 bytes
コンパイル時間 12,769 ms
コンパイル使用メモリ 404,988 KB
実行使用メモリ 10,112 KB
最終ジャッジ日時 2024-05-17 22:00:19
合計ジャッジ時間 13,986 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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