From a370d5a7236b669913afb7a225eef796134297c6 Mon Sep 17 00:00:00 2001 From: unlockable Date: Sun, 5 Nov 2023 01:24:20 +0800 Subject: [PATCH] Example not correct. --- .clang-format | 2 +- 2023204/main.cpp | 150 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 2023204/main.cpp diff --git a/.clang-format b/.clang-format index f91889e..16c54a0 100644 --- a/.clang-format +++ b/.clang-format @@ -77,7 +77,7 @@ BreakConstructorInitializersBeforeComma: false BreakConstructorInitializers: BeforeColon BreakAfterJavaFieldAnnotations: false BreakStringLiterals: true -ColumnLimit: 80 +ColumnLimit: 100 CommentPragmas: '^ IWYU pragma:' QualifierAlignment: Leave CompactNamespaces: false diff --git a/2023204/main.cpp b/2023204/main.cpp new file mode 100644 index 0000000..fab214c --- /dev/null +++ b/2023204/main.cpp @@ -0,0 +1,150 @@ +#include +#define DEAD false +#define ALIVE true +// 'Natural count' is the name of the node when all the nodes in the tree is +// alive. xxx_num denotes the count as the question described. + +int ans[30] = {0}; +int dead_nodes_num[105] = {0}; +int total_dead_nodes = 0; +int target_nodes_num[105] = {0}; +int total_target_nodes = 0; + +struct SeveralContinuedDeadNode { + bool status; + int length; // Caution that when status == dead and length == 1, the node + // itself is alive. +}; + +struct SearchResult { + int next_ans_pos; // pos in the ans[] array. + int count_in_father_line; // natural count. +}; + +SearchResult find_node(int target, int start_num, int next_dead_node_num_ptr, + SeveralContinuedDeadNode *upper_line, int upper_line_segment_count, + int layer) { + int spawned_node_max_num = start_num - 1; + int segment_count = upper_line_segment_count; + SeveralContinuedDeadNode current_line_node[100] = {0}; + int current_line_len = 0; + bool all_dead = true; + + // Spawing all the nodes in current line + for (int upper_line_iter = 0; upper_line_iter < upper_line_segment_count; upper_line_iter++) { + if (upper_line[upper_line_iter].status == DEAD) { + current_line_node[current_line_len] = upper_line[upper_line_iter]; + current_line_node[current_line_len].length *= 2; + current_line_len++; + } + else { + int total_spawned_after = spawned_node_max_num + upper_line[upper_line_iter].length * 2; + while (dead_nodes_num[next_dead_node_num_ptr] <= total_spawned_after && + next_dead_node_num_ptr < total_dead_nodes) { + if (dead_nodes_num[next_dead_node_num_ptr] - spawned_node_max_num > 1) { + current_line_node[current_line_len] = SeveralContinuedDeadNode{ + ALIVE, dead_nodes_num[next_dead_node_num_ptr] - spawned_node_max_num - 1}; + current_line_len++; // things before this new dead node + all_dead = false; + } + current_line_node[current_line_len] = + SeveralContinuedDeadNode{DEAD, 1}; // this new dead node + current_line_len++; + + spawned_node_max_num = dead_nodes_num[next_dead_node_num_ptr]; // we now spawned to + // this dead node. + next_dead_node_num_ptr++; + } + if (spawned_node_max_num < total_spawned_after) { + current_line_node[current_line_len] = + SeveralContinuedDeadNode{ALIVE, total_spawned_after - spawned_node_max_num}; + current_line_len++; + all_dead = false; + } + + spawned_node_max_num = total_spawned_after; + } + } + + if (all_dead) { + return SearchResult{1, -1}; + } + + if (target <= spawned_node_max_num) { + ans[0] = target; + int natural_pos = 0, num = start_num; + for (int i = 0; i < current_line_len; i++) { + if (current_line_node[i].status == DEAD) { + natural_pos += current_line_node[i].length; + num += (current_line_node[i].length == 1); + if (num == target) { + return SearchResult {1, natural_pos / 2}; + } + continue; + } + + // Now deal with alive nodes + if (num + current_line_node[i].length < target) { + natural_pos += current_line_node[i].length; + num += current_line_node[i].length; + } + else { + // if we made it here, then the target <= num + current_line_node[i].length + return SearchResult{1, (natural_pos + target - num) / 2}; + } + } + return SearchResult{1, -1}; + } + else { + SearchResult current_line_pos = + find_node(target, spawned_node_max_num + 1, next_dead_node_num_ptr, current_line_node, + current_line_len, layer + 1); + if (current_line_pos.count_in_father_line < 0) { + // The path does not exist. + return SearchResult{1, -1}; + } + + int natural_count = 0, num = start_num; + for (int i = 0; i < current_line_len; i++) { + natural_count += current_line_node[i].length; + if (current_line_node[i].status == ALIVE || current_line_node[i].length == 1) { + num += current_line_node[i].length; + } + + if (natural_count > current_line_pos.count_in_father_line) { + num -= natural_count - current_line_pos.count_in_father_line; + ans[current_line_pos.next_ans_pos++] = num; + break; + } + } + return SearchResult{current_line_pos.next_ans_pos, + current_line_pos.count_in_father_line / 2}; + } +} + +int main() { + scanf("%d %d", &total_dead_nodes, &total_target_nodes); + for (int i = 0; i < total_dead_nodes; i++) { + scanf("%d", &dead_nodes_num[i]); + } + if (dead_nodes_num[0] == 1) { + for (int i = 0; i < total_target_nodes; i++) { + printf("0\n"); + } + } + else { + int current_target = 0; + SeveralContinuedDeadNode firstline = SeveralContinuedDeadNode{ALIVE, 1}; + for (int i = 0; i < total_target_nodes; i++) { + scanf("%d", ¤t_target); + SearchResult result = find_node(current_target, 2, 0, &firstline, 1, 2); + printf("1 "); + for (int j = result.next_ans_pos - 1; j >= 0; j--) { + printf("%d ", ans[j]); + } + printf("\n"); + } + } + + return 0; +} \ No newline at end of file