Advertisement
vlatkovski

ioi 2018 mechanical doll (WIP)

Feb 18th, 2019
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.38 KB | None | 0 0
  1. #include "doll.h"
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5.  
  6.  
  7. const int UNSET = -69696969;
  8.  
  9. void create_circuit(int M, vector<int> A) {
  10.     int N = A.size();
  11.     int S = 1;
  12.     while (S <= N+1) S *= 2;
  13.     --S;
  14.  
  15.     vector<int> C(M+1);
  16.     for (int i = 0; i <= M; ++i) {
  17.         C[i] = -1;
  18.     }
  19.  
  20.     vector<int> X(S, UNSET), Y(S, UNSET);
  21.     vector<int> parent(S, UNSET);
  22.     vector<int> numv(S, 0);
  23.     int extra = S - N;
  24.  
  25.     cout << "N=" << N << "\nS=" << S << endl;
  26.     cout << "extra=" << extra << "\n";
  27.     parent[-(-1)-1] = 0;
  28.     for (int n = -1; 2*(-n) <= S; --n) {
  29.         X[-n-1] = 2*n;
  30.         Y[-n-1] = 2*n-1;
  31.         parent[2*(-n)-1] = n;
  32.         parent[2*(-n)] = n;
  33.     }
  34.     for (int n = -(S/2 + 1); -n <= S; --n) {
  35.         if (extra == 0) break;
  36.         if (X[-n-1] == UNSET) {
  37.             X[-n-1] = -1;
  38.             --extra;
  39.         }
  40.         if (extra == 0) break;
  41.         if (Y[-n-1] == UNSET) {
  42.             Y[-n-1] = -1;
  43.             --extra;
  44.         }
  45.     }
  46.     Y[-(-S)-1] = 0;
  47.     cout << endl;
  48.     for (int i = 0; i < N; ++i) {
  49.         int n = -1;
  50.         cout << "i=" << i << " A[i]=" << A[i] << endl;
  51.         ++numv[-n-1];
  52.         while (X[-n-1] != UNSET && Y[-n-1] != UNSET) {
  53.             if (numv[-n-1] % 2 == 1) {
  54.                 n = X[-n-1];
  55.             } else {
  56.                 n = Y[-n-1];
  57.             }
  58.             ++numv[-n-1];
  59.             cout << "n=" << n << endl;
  60.         }
  61.         cout << "\n";
  62.         if (X[-n-1] == UNSET) {
  63.             X[-n-1] = A[i];
  64.         } else {
  65.             Y[-n-1] = A[i];
  66.         }
  67.     }
  68.     for (int n = -S; n <= -1; ++n) {
  69.         cout << "n=" << n << "; pr=" << parent[-n-1] << " chld=" << X[-n-1] << "," << Y[-n-1] << endl;
  70.         if (X[-n-1] == Y[-n-1]/* || X[-n-1] == -1 || Y[-n-1] == -1*/) {
  71.             int trg = X[-n-1] == -1 ? Y[-n-1] : X[-n-1];
  72.             cout << "redundant: trg=" << trg << "\n";
  73.             if (n == X[-parent[-n-1]-1]) {
  74.                 X[-parent[-n-1]-1] = trg;
  75.             } else {
  76.                 Y[-parent[-n-1]-1] = trg;
  77.             }
  78.         }
  79.     }
  80.  
  81.     for (int n = -1; -n <= S; --n) {
  82.         numv[-n-1] = 0;
  83.     }
  84.  
  85.     int S1 = 0;
  86.     vector<int> newid(S, UNSET);
  87.     vector<int> oldid(S, UNSET);
  88.  
  89.     cout << "\nSIMULATION:\n";
  90.  
  91.     {
  92.         int n = 0;
  93.         do {
  94.             cout << "n=" << n << endl;
  95.             if (n >= 0) {
  96.                 n = C[n];
  97.             } else {
  98.                 if (newid[-n-1] == UNSET) {
  99.                     cout << "new, S1=" << S1 << endl;
  100.                     ++S1;
  101.                     newid[-n-1] = -S1;
  102.                     oldid[S1-1] = n;
  103.                 }
  104.                 ++numv[-n-1];
  105.                 if (numv[-n-1] % 2 == 1) {
  106.                     n = X[-n-1];
  107.                 } else {
  108.                     n = Y[-n-1];
  109.                 }
  110.             }
  111.         } while (n != 0);
  112.     }
  113.  
  114.     cout << "\nNEW:\n";
  115.     cout << "S1=" << S1 << endl;
  116.  
  117.     for (int n = -1; n >= -S; --n) {
  118.         cout << "n=" << n << " newid=" << newid[-n-1] << "  oldid=" << oldid[-n-1] << endl;
  119.     }
  120.     cout << endl;
  121.  
  122.     vector<int> X1(S1), Y1(S1);
  123.     for (int n = -1; n >= -S1; --n) {
  124.         X1[-n-1] = newid[-X[-oldid[-n-1]-1]-1];
  125.         Y1[-n-1] = newid[-Y[-oldid[-n-1]-1]-1];
  126.         cout << "n=" << n << " X=" << X1[-n-1] << " Y=" << Y1[-n-1] << endl;
  127.     }
  128.  
  129.     answer(C, X1, Y1);
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement