Dang_Quan_10_Tin

SWAPGAME

Nov 3rd, 2021 (edited)
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <vector>
  5. #include <complex>
  6. #include <iomanip>
  7. #include <cstring>
  8.  
  9. #define task "SWAPGAME"
  10. using namespace std;
  11. using ll = long long;
  12. using ld = long double;
  13.  
  14. using cd = complex<long double>;
  15. const long double PI = acos(-1);
  16. const int M = 2e5;
  17. const ll BASE = 1e15;
  18. const int gd = log10(BASE);
  19. const int maxn = M / gd + 1;
  20. struct Bignum
  21. {
  22.     int n;
  23.     ll a[maxn];
  24.     Bignum(ll x = 0)
  25.     {
  26.         memset(a, 0, sizeof a);
  27.         n = 0;
  28.         do
  29.         {
  30.             a[n++] = x % BASE;
  31.             x /= BASE;
  32.         } while (x);
  33.     }
  34.     void fix()
  35.     {
  36.         ++n;
  37.         for (int i = 0; i < n - 1; ++i)
  38.         {
  39.             a[i + 1] += a[i] / BASE;
  40.             a[i] %= BASE;
  41.             if (a[i] < 0)
  42.             {
  43.                 a[i] += BASE;
  44.                 --a[i + 1];
  45.             }
  46.         }
  47.         while (n > 1 && a[n - 1] == 0)
  48.             --n;
  49.     }
  50.     Bignum &operator*=(const ll &x)
  51.     {
  52.         for (int i = 0; i < n; ++i)
  53.             a[i] *= x;
  54.         fix();
  55.         return *this;
  56.     }
  57.     friend ostream &operator<<(ostream &out, const Bignum &x)
  58.     {
  59.         int i = x.n;
  60.         while (i > 0 && x.a[i] == 0)
  61.             --i;
  62.         out << x.a[i];
  63.         for (--i; ~i; --i)
  64.             out << setw(gd) << setfill('0') << x.a[i];
  65.         return out;
  66.     }
  67. };
  68.  
  69. const int N = 1e5 + 5;
  70. int a[N], b[N], n, l(0);
  71. bool vis[N];
  72. vector<int> adj[N], same[N], loop[N];
  73.  
  74. void Read()
  75. {
  76.     cin >> n;
  77.     for (int i = 1; i <= n; ++i)
  78.         cin >> a[i],
  79.             same[a[i]].push_back(i);
  80.     for (int i = 1; i <= n; ++i)
  81.         cin >> b[i],
  82.             same[b[i]].push_back(i);
  83. }
  84.  
  85. void dfs(int v)
  86. {
  87.     vis[v] = 1;
  88.     loop[l].push_back(v);
  89.     int cnt = 0;
  90.     for (auto i : adj[v])
  91.         if (!vis[i] && cnt == 0)
  92.         {
  93.             ++cnt;
  94.             dfs(i);
  95.         }
  96. }
  97.  
  98. Bignum Pow(ll a, ll b)
  99. {
  100.     Bignum ans(1);
  101.     while (b--)
  102.         ans *= a;
  103.     return ans;
  104. }
  105.  
  106. void Solve()
  107. {
  108.     for (int i = 1; i <= n; ++i)
  109.         if (same[i][0] != same[i][1])
  110.         {
  111.             adj[same[i][0]].push_back(same[i][1]);
  112.             adj[same[i][1]].push_back(same[i][0]);
  113.         }
  114.     for (int i = 1; i <= n; ++i)
  115.         if (!vis[i] && adj[i].size() != 0)
  116.         {
  117.             ++l;
  118.             dfs(i);
  119.         }
  120.  
  121.     cout << Pow(2, l) << "\n";
  122.  
  123.     int ans = 0;
  124.     for (int i = 1; i <= l; ++i)
  125.     {
  126.         int cur = 0;
  127.         for (int j = 1; j < loop[i].size(); ++j)
  128.         {
  129.             int t = loop[i][j],
  130.                 h = loop[i][j - 1];
  131.             if (a[t] == a[h] || b[t] == b[h])
  132.             {
  133.                 ++cur;
  134.                 swap(a[t], b[t]);
  135.             }
  136.         }
  137.         ans += min(cur, (int)loop[i].size() - cur);
  138.     }
  139.     cout << ans;
  140. }
  141.  
  142. int main()
  143. {
  144.     ios_base::sync_with_stdio(0);
  145.     cin.tie(0);
  146.     cout.tie(0);
  147.     if (fopen(task ".INP", "r"))
  148.     {
  149.         freopen(task ".INP", "r", stdin);
  150.         freopen(task ".OUT", "w", stdout);
  151.     }
  152.     Read();
  153.     Solve();
  154. }
Add Comment
Please, Sign In to add comment