Home BOJ. Perfect Binary Tree (9934)
Post
Cancel

BOJ. Perfect Binary Tree (9934)

[Link] https://www.acmicpc.net/problem/9934


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cstring>
// #include <bits/stdc++.h>
using namespace std;
#define for0(i, n) for(int i = 0; i < n; i++)
#define for1(i, n) for(int i = 1; i < n; i++)
#define fori(s, e) for(int i = s; i < e; i++)
#define endl "\n"
#define nulls '\0'
#define dkdk " "

int k;
// vector<int> ans;
vector<int> record;

void conquer(vector<int> *ans, int left, int right, int idx) {
  if(left == right) {
    (*ans)[idx-1] = record[left - 1];
    return;
  }
  int mid = (left + right) >> 1;
  (*ans)[idx-1] = record[mid - 1];
  conquer(ans, left, mid - 1, 2*idx);
  conquer(ans, mid + 1, right, 2*idx+1);
}

void solve(vector<int> *ans) {
  ans->resize((1 << k) - 1);
  conquer(ans, 1, (1 << k) - 1, 1);
}

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  // freopen("input.txt", "r", stdin);
  cin >> k; int i = (1 << k) - 1;
  while(i--) { int in; cin >> in; record.push_back(in); }
  i = 0;
  vector<int> ans;
  solve(&ans);
  int stair = 1;
  int idx = 0;
  for(int stair = 1; stair <= k; stair++) {
    for0(i, 1 << (stair-1)) cout << ans[idx++] << dkdk;
    cout << endl;
  }
  return 0;
}
This post is licensed under CC BY 4.0 by the author.