Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- cin.tie(0)->sync_with_stdio(0);
- int p, u; cin >> p >> u;
- int n = p + u;
- vector<int> a(n);
- for (int &x : a)
- cin >> x;
- for (int i = 0; i < p; ++i) {
- a[i] = -a[i];
- }
- int ansP, ansU;
- cin >> ansP >> ansU;
- vector<int> b(n);
- for (int &x : b) {
- cin >> x;
- }
- for (int i = 0; i < ansP; ++i) {
- b[i] = -b[i];
- }
- map<vector<int>, int> dist;
- queue<vector<int>> q;
- q.push(a);
- dist[a] = 0;
- auto eq = [](vector<int> &a, vector<int> &b) {
- if (a.size() != b.size()) return false;
- for (int i = 0; i < (int)a.size(); ++i) {
- if (a[i] != b[i]) return false;
- }
- return true;
- };
- while (!q.empty()) {
- auto u = q.front(); q.pop();
- // for (int x : u) {
- // cout << x << ' ';
- // }
- // cout << '\n';
- // assert(u.size() == n);
- if (eq(u, b)) break;
- for (int i = 0; i < n; ++i) {
- if (u[i] > 0) {
- // unpinned
- vector<int> v;
- bool ok = false;
- for (int j = 0; j < n; ++j) if (i != j) {
- if (u[j] > 0 and !ok) {
- v.push_back(-u[i]);
- ok = true;
- }
- if (u[j] > 0) {
- v.push_back(u[j]);
- } else {
- v.push_back(u[j]);
- }
- }
- if (!ok) v.push_back(-u[i]);
- if (!dist.count(v)) {
- dist[v] = dist[u] + 1;
- q.push(v);
- }
- } else {
- // pinned
- vector<int> v;
- bool ok = false;
- for (int j = 0; j < n; ++j) if (i != j) {
- if (u[j] > 0 and !ok) {
- v.push_back(-u[i]);
- ok = true;
- }
- if (u[j] > 0) {
- v.push_back(u[j]);
- } else {
- v.push_back(u[j]);
- }
- }
- if (!ok) v.push_back(-u[i]);
- if (!dist.count(v)) {
- dist[v] = dist[u] + 1;
- q.push(v);
- }
- }
- }
- }
- cout << dist[b] << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment