danielvitor23

I. Pinned Files

Oct 8th, 2023
1,000
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5.   cin.tie(0)->sync_with_stdio(0);
  6.  
  7.   int p, u; cin >> p >> u;
  8.   int n = p + u;
  9.  
  10.   vector<int> a(n);
  11.   for (int &x : a)
  12.     cin >> x;
  13.  
  14.   for (int i = 0; i < p; ++i) {
  15.     a[i] = -a[i];
  16.   }
  17.  
  18.   int ansP, ansU;
  19.   cin >> ansP >> ansU;
  20.  
  21.   vector<int> b(n);
  22.   for (int &x : b) {
  23.     cin >> x;
  24.   }
  25.  
  26.   for (int i = 0; i < ansP; ++i) {
  27.     b[i] = -b[i];
  28.   }
  29.  
  30.   map<vector<int>, int> dist;
  31.   queue<vector<int>> q;
  32.   q.push(a);
  33.   dist[a] = 0;
  34.  
  35.   auto eq = [](vector<int> &a, vector<int> &b) {
  36.     if (a.size() != b.size()) return false;
  37.     for (int i = 0; i < (int)a.size(); ++i) {
  38.       if (a[i] != b[i]) return false;
  39.     }
  40.     return true;
  41.   };
  42.  
  43.   while (!q.empty()) {
  44.     auto u = q.front(); q.pop();
  45.     // for (int x : u) {
  46.     //   cout << x << ' ';
  47.     // }
  48.     // cout << '\n';
  49.     // assert(u.size() == n);
  50.  
  51.     if (eq(u, b)) break;
  52.  
  53.     for (int i = 0; i < n; ++i) {
  54.       if (u[i] > 0) {
  55.         // unpinned
  56.         vector<int> v;
  57.         bool ok = false;
  58.         for (int j = 0; j < n; ++j) if (i != j) {
  59.           if (u[j] > 0 and !ok) {
  60.             v.push_back(-u[i]);
  61.             ok = true;
  62.           }
  63.           if (u[j] > 0) {
  64.             v.push_back(u[j]);
  65.           } else {
  66.             v.push_back(u[j]);
  67.           }
  68.         }
  69.         if (!ok) v.push_back(-u[i]);
  70.         if (!dist.count(v)) {
  71.           dist[v] = dist[u] + 1;
  72.           q.push(v);
  73.         }
  74.       } else {
  75.         // pinned
  76.         vector<int> v;
  77.         bool ok = false;
  78.         for (int j = 0; j < n; ++j) if (i != j) {
  79.           if (u[j] > 0 and !ok) {
  80.             v.push_back(-u[i]);
  81.             ok = true;
  82.           }
  83.           if (u[j] > 0) {
  84.             v.push_back(u[j]);
  85.           } else {
  86.             v.push_back(u[j]);
  87.           }
  88.         }
  89.         if (!ok) v.push_back(-u[i]);
  90.         if (!dist.count(v)) {
  91.           dist[v] = dist[u] + 1;
  92.           q.push(v);
  93.         }
  94.       }
  95.     }
  96.   }
  97.  
  98.   cout << dist[b] << '\n';
  99. }
Advertisement
Add Comment
Please, Sign In to add comment