konchin_shih

c073: 00101 - The Blocks Problem

Aug 7th, 2020
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.21 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #include<algorithm>
  4. #define endl '\n'
  5. using namespace std;
  6. int main() {
  7.     cin.tie(nullptr);
  8.     ios::sync_with_stdio(false);
  9.     int n, a, b;
  10.     string str1, str2;
  11.     while (cin >> n) {
  12.         vector<pair<size_t, size_t>> p(n);
  13.         vector<vector<int>> v(n);
  14.         for (int i = 0; i != n; i++)
  15.             v[i].assign({i}), p[i] = {i, 0};
  16.         while (cin >> str1) {
  17.             if (str1 == "quit") break;
  18.             cin >> a >> str2 >> b;
  19.             if (a == b || p[a].first == p[b].first) continue;
  20.             auto pb = [&](auto pos) {
  21.                 auto x = p[pos].first, y = p[pos].second + 1;
  22.                 vector<int> tmp(v[x].begin() + y, v[x].end());
  23.                 v[x].erase(v[x].begin() + y, v[x].end());
  24.                 for (const auto& i : tmp)
  25.                     v[i].emplace_back(i), p[i] = {i, v[i].size() - 1};
  26.             };
  27.             if (str1 == "move") pb(a);
  28.             if (str2 == "onto") pb(b);
  29.             auto x = p[a].first, y = p[a].second;
  30.             vector<int> tmp(v[x].begin() + y, v[x].end());
  31.             v[x].erase(v[x].begin() + y, v[x].end());
  32.             x = p[b].first;
  33.             for (const auto& i : tmp)
  34.                 v[x].emplace_back(i), p[i] = {x, v[x].size() - 1};
  35.         }
  36.         for (size_t i = 0; i < v.size(); i++) {
  37.             cout << i << ": ";
  38.             for (const auto& j : v[i])
  39.                 cout << j << ' ';
  40.             cout << endl;
  41.         }
  42.     }
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment