Advertisement
mickypinata

IOI18: Mechanical Doll

Nov 25th, 2020
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include "doll.h"
  3. using namespace std;
  4.  
  5. vector<int> X(0), Y(0);
  6.  
  7. int seq = 0;
  8. int createSwitch(int x, int y){
  9.     X.push_back(x);
  10.     Y.push_back(y);
  11.     --seq;
  12.     return seq;
  13. }
  14.  
  15. int createTree(vector<int> &V){
  16.     int n = V.size();
  17.     if(n == 1){
  18.         return V[0];
  19.     }
  20.     bool same = true;
  21.     for(int i = 0; i < n - 1; ++i){
  22.         if(V[i] != V[i + 1]){
  23.             same = false;
  24.             break;
  25.         }
  26.     }
  27.     if(same){
  28.         return V[0];
  29.     }
  30.     vector<int> x(0), y(0);
  31.     for(int i = 0; i < n; ++i){
  32.         if(i % 2 == 0){
  33.             x.push_back(V[i]);
  34.         } else {
  35.             y.push_back(V[i]);
  36.         }
  37.     }
  38.     int ix = createTree(x);
  39.     int iy = createTree(y);
  40.     return createSwitch(ix, iy);
  41. }
  42.  
  43. int revBit(int a, int k){
  44.     int b = 0;
  45.     while(k--){
  46.         b <<= 1;
  47.         b |= (a & 1);
  48.         a >>= 1;
  49.     }
  50.     return b;
  51. }
  52.  
  53. void create_circuit(int M, vector<int> A){
  54.     int n = A.size();
  55.     int nExp = 0;
  56.     while((1 << nExp) < n){
  57.         ++nExp;
  58.     }
  59.     A.push_back(0);
  60.     vector<int> V(1 << nExp, 0);
  61.     for(int i = 0; i < (1 << nExp) - n; ++i){
  62.         V[revBit(i, nExp)] = 1e9;
  63.     }
  64.     int idx = 1;
  65.     for(int i = 0; i < (1 << nExp); ++i){
  66.         if(V[i] != 0){
  67.             continue;
  68.         }
  69.         V[i] = A[idx];
  70.         ++idx;
  71.     }
  72.     int root = createTree(V);
  73.  
  74.     vector<int> C(M + 1, root);
  75.     C[0] = A[0];
  76.     for(auto &x : X){
  77.         if(x == 1e9){
  78.             x = root;
  79.         }
  80.     }
  81.     for(auto &y : Y){
  82.         if(y == 1e9){
  83.             y = root;
  84.         }
  85.     }
  86.  
  87.     answer(C, X, Y);
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement