시나브로

[D3] 7985. Rooted Binary Tree 재구성 본문

알고리즘/SW Expert Academy

[D3] 7985. Rooted Binary Tree 재구성

혬혬 2019. 10. 6. 11:05
728x90
#include <stdio.h>
int tree[3000];
int each_depth[12][1025];
int each_depth_index[12];
int max_depth = 0;
int squeared_2[11] = { 0 };
void seach_depth(int start, int end, int depth) {
	if (depth > max_depth)
		return;
	int box = (end-start) / 2;
	each_depth[depth][each_depth_index[depth]++] = tree[start + box];
	seach_depth(start, start + box - 1, depth + 1);
	seach_depth(start + box+1, end, depth + 1);
}
int main() {

	int test_case = 0;
	scanf("%d", &test_case);
	squeared_2[0] = 1;
	for (int i = 1; i <= 11; i++) {
		squeared_2[i] = squeared_2[i - 1] * 2;
	}
	for (int q = 0; q < test_case; q++) {
		scanf("%d", &max_depth);
		for (int i = 0; i < squeared_2[max_depth] - 1; i++) {
			scanf("%d", &tree[i]);
		}
		seach_depth(0, squeared_2[max_depth]-2, 1);
		printf("#%d ", q + 1);
		int tree_index = 0;
		for (int i = 1; i <= max_depth; i++) {
			for (int j = 0; j < each_depth_index[i]; j++) {
				printf("%d ", each_depth[i][j]);
				each_depth[i][j] = 0;
				tree[tree_index++] = 0;
			}
			each_depth_index[i] = 0;
			printf("\n");
		}
	}

	return 0;
}

 

728x90

'알고리즘 > SW Expert Academy' 카테고리의 다른 글

[D4] 7965. 퀴즈  (0) 2019.10.06
[D3] 8016. 홀수 피라미드  (0) 2019.10.06
[D3] 8104. 조 만들기  (0) 2019.10.06
[D5] 7730. 나무 깎는 홍준  (0) 2019.10.06
[D4] 7829. 보물왕 태혁  (0) 2019.10.06
Comments