結果
問題 | No.2759 Take Pictures, Elements? |
ユーザー |
|
提出日時 | 2024-05-17 22:00:02 |
言語 | Rust (1.83.0 + proconio) |
結果 |
AC
|
実行時間 | 28 ms / 2,000 ms |
コード長 | 895 bytes |
コンパイル時間 | 15,876 ms |
コンパイル使用メモリ | 393,068 KB |
実行使用メモリ | 10,112 KB |
最終ジャッジ日時 | 2024-12-20 13:48:09 |
合計ジャッジ時間 | 17,164 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 21 |
ソースコード
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);}