結果

問題 No.2759 Take Pictures, Elements?
ユーザー atcoder8atcoder8
提出日時 2024-05-17 23:04:57
言語 Rust
(1.77.0)
結果
AC  
実行時間 6 ms / 2,000 ms
コード長 867 bytes
コンパイル時間 12,816 ms
コンパイル使用メモリ 401,364 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-17 23:05:11
合計ジャッジ時間 12,339 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,816 KB
testcase_01 AC 6 ms
6,820 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,944 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 4 ms
6,940 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 3 ms
6,944 KB
testcase_12 AC 3 ms
6,940 KB
testcase_13 AC 4 ms
6,944 KB
testcase_14 AC 4 ms
6,944 KB
testcase_15 AC 4 ms
6,944 KB
testcase_16 AC 4 ms
6,940 KB
testcase_17 AC 3 ms
6,940 KB
testcase_18 AC 3 ms
6,940 KB
testcase_19 AC 4 ms
6,940 KB
testcase_20 AC 3 ms
6,944 KB
testcase_21 AC 1 ms
6,940 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

use proconio::input;

const INF: usize = 10_usize.pow(6);

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

    let mut dp = vec![INF; n];
    dp[0] = 0;
    for &b in &bb {
        let mut next_dp = vec![INF; n];

        let mut to_right = INF;
        for (i, &a) in aa.iter().enumerate() {
            to_right = to_right.min(dp[i] + n - 1 - i);

            if a == b {
                next_dp[i] = next_dp[i].min(to_right - (n - 1 - i));
            }
        }

        let mut to_left = INF;
        for (i, &a) in aa.iter().enumerate().rev() {
            to_left = to_left.min(dp[i] + i);

            if a == b {
                next_dp[i] = next_dp[i].min(to_left - i);
            }
        }

        dp = next_dp;
    }

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