結果
| 問題 |
No.2759 Take Pictures, Elements?
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-05-17 22:32:47 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 36 ms / 2,000 ms |
| コード長 | 1,034 bytes |
| コンパイル時間 | 1,745 ms |
| コンパイル使用メモリ | 211,212 KB |
| 実行使用メモリ | 7,844 KB |
| 最終ジャッジ日時 | 2025-06-20 13:18:24 |
| 合計ジャッジ時間 | 2,828 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 21 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const ll INF = 1e18;
void solve() {
ll n, q;
cin >> n >> q;
map<ll, set<ll>> mp;
rep(i, n) {
ll a;
cin >> a;
if (mp.count(a) == 0)
mp[a].insert(-1);
mp[a].insert(i);
}
for (auto &[k, v] : mp)
v.insert(n);
vector<ll> dp(n, INF);
dp[0] = 0;
rep(qi, q) {
ll b;
cin >> b;
vector<ll> nex(n, INF);
rep(ci, n) {
auto iter = mp[b].lower_bound(ci);
auto upd = [&]() {
nex[*iter] = min(nex[*iter], dp[ci] + abs(*iter - ci));
};
if (ci == *iter) {
upd();
continue;
}
if (*iter < n)
upd();
iter--;
if (0 <= *iter)
upd();
}
dp = nex;
}
ll ans = INF;
rep(i, n) ans = min(ans, dp[i]);
cout << ans << '\n';
}
int main() {
std::cin.tie(nullptr);
std::ios_base::sync_with_stdio(false);
int T = 1;
for (int t = 0; t < T; t++) {
solve();
}
return 0;
}