結果
問題 | No.2759 Take Pictures, Elements? |
ユーザー | 👑 binap |
提出日時 | 2024-05-01 04:25:41 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 334 ms / 2,000 ms |
コード長 | 3,066 bytes |
コンパイル時間 | 4,468 ms |
コンパイル使用メモリ | 279,164 KB |
実行使用メモリ | 105,756 KB |
最終ジャッジ日時 | 2024-05-09 19:54:52 |
合計ジャッジ時間 | 8,183 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 284 ms
90,336 KB |
testcase_01 | AC | 334 ms
105,756 KB |
testcase_02 | AC | 2 ms
5,376 KB |
testcase_03 | AC | 1 ms
5,376 KB |
testcase_04 | AC | 1 ms
5,376 KB |
testcase_05 | AC | 2 ms
5,376 KB |
testcase_06 | AC | 2 ms
5,376 KB |
testcase_07 | AC | 2 ms
5,376 KB |
testcase_08 | AC | 2 ms
5,376 KB |
testcase_09 | AC | 183 ms
89,436 KB |
testcase_10 | AC | 183 ms
89,324 KB |
testcase_11 | AC | 184 ms
89,452 KB |
testcase_12 | AC | 188 ms
89,320 KB |
testcase_13 | AC | 185 ms
89,448 KB |
testcase_14 | AC | 260 ms
89,284 KB |
testcase_15 | AC | 262 ms
89,404 KB |
testcase_16 | AC | 263 ms
89,408 KB |
testcase_17 | AC | 268 ms
89,276 KB |
testcase_18 | AC | 261 ms
89,412 KB |
testcase_19 | AC | 266 ms
89,280 KB |
testcase_20 | AC | 268 ms
89,408 KB |
testcase_21 | AC | 2 ms
5,376 KB |
testcase_22 | AC | 2 ms
5,376 KB |
testcase_23 | AC | 2 ms
5,376 KB |
ソースコード
#include<bits/stdc++.h> #include<atcoder/all> #define rep(i,n) for(int i=0;i<n;i++) using namespace std; using namespace atcoder; typedef long long ll; typedef vector<int> vi; typedef vector<long long> vl; typedef vector<vector<int>> vvi; typedef vector<vector<long long>> vvl; typedef long double ld; typedef pair<int, int> P; ostream& operator<<(ostream& os, const modint& a) {os << a.val(); return os;} template <int m> ostream& operator<<(ostream& os, const static_modint<m>& a) {os << a.val(); return os;} template <int m> ostream& operator<<(ostream& os, const dynamic_modint<m>& a) {os << a.val(); return os;} template<typename T> istream& operator>>(istream& is, vector<T>& v){int n = v.size(); assert(n > 0); rep(i, n) is >> v[i]; return is;} template<typename U, typename T> ostream& operator<<(ostream& os, const pair<U, T>& p){os << p.first << ' ' << p.second; return os;} template<typename T> ostream& operator<<(ostream& os, const vector<T>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : " "); return os;} template<typename T> ostream& operator<<(ostream& os, const vector<vector<T>>& v){int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "\n" : ""); return os;} template<typename T> void chmin(T& a, T b){a = min(a, b);} template<typename T> void chmax(T& a, T b){a = max(a, b);} struct Edge_BFS01{ int from, to; int cost; Edge_BFS01(int from, int to, int cost) : from(from), to(to), cost(cost) {}; }; const int INF = 1001001001; struct BFS01{ int n, m; vector<bool> initialized; vector<Edge_BFS01> E; vector<vector<int>> G; map<int, vector<int>> dist; map<int, vector<int>> idx; BFS01(int _n) : n(_n), m(0), initialized(n, false), G(n){} void add_edge(int from, int to, int cost){ assert(cost == 0 or cost == 1); Edge_BFS01 e(from, to, cost); E.push_back(e); G[from].emplace_back(m); m++; } void calc(int s){ initialized[s] = true; dist[s] = vector<int>(n, INF); idx[s] = vector<int>(n, -1); deque<tuple<int, int ,int>> de; de.emplace_back(0, s, -1); while(de.size()){ auto [cost, from, index] = de.front(); de.pop_front(); if(dist[s][from] <= cost) continue; dist[s][from] = cost; idx[s][from] = index; for(int index : G[from]){ int to = E[index].to; int cost_plus = E[index].cost; if(dist[s][to] <= cost + cost_plus) continue; if(cost_plus == 0) de.emplace_front(cost + cost_plus, to, index); if(cost_plus == 1) de.emplace_back(cost + cost_plus, to, index); } } } int get_dist(int s, int t){ if(!initialized[s]) calc(s); return dist[s][t]; } }; int main(){ int n, q; cin >> n >> q; vector<int> a(n); vector<int> b(q); cin >> a >> b; BFS01 graph(n * q + 1); int tv = n * q; rep(j, q){ rep(i, n - 1){ graph.add_edge(j * n + i, j * n + (i + 1), 1); graph.add_edge(j * n + (i + 1), j * n + i, 1); } rep(i, n){ if(a[i] == b[j]){ if(j != q - 1) graph.add_edge(j * n + i, (j + 1) * n + i, 0); else graph.add_edge(j * n + i, tv, 0); } } } int ans = graph.get_dist(0, tv); cout << ans << "\n"; return 0; }