# 50105. Seesaw Chandelier Tree

## I'm a slow walker, but I never walk backwards.

Write a function to construct a seesaw chandelier tree.

A seesaw chandelier is a hierarchy of seesaws in which every seesaw is balanced. A seesaw chandelier can be recursively defined as follow. A balancing point is defined as in the previous tasks.

• If there is a balancing point, then the balancing point links to another seesaw chandelier with a "left" pointer, and links to the other seesaw chandelier with a "right" pointer.
• If there is no balancing point, then a seesaw chandelier has a sequence of nodes linked by the "left" pointer as a linked list.

We want to build a seesaw chandelier tree from a sequence of $n$ non-negative integers, indexed from $0$ to $n-1$. For the first level, we consider the whole sequence as a seesaw and find the balancing point at index $i$ between $0$ to $n-1$, as we did in the previous tasks. Now the root of the tree is the balancing point at index $i$ and two partitions -- one on the left tree and one on the right tree. The one on the left has index from $0$ to $i-1$, and the one on the right has index from $i+1$ to $n-1$. Now we consider these two partitions as two independent seesaw hierarchies and find the balancing points for them. We can recursively do this until the length is less than $3$, or we cannot find a balancing point for a partition. In both cases we stop the recursion and use left pointer to link all the nodes in the partition from right to left. Please refer to the second condition in the previous recursive definition.

For example, we now want to build a seesaw chandelier with a sequence of $8$ non-negative integers, $(2, 5, 2, 0, 7, 1, 7, 4)$. At the first level, we can find the balancing point at $7$ (with index $4$), because $2 \times 4 + 5 \times 3 + 2 \times 2 + 0 \times 1 = 1 \times 1 + 7 \times 2 + 4 \times 3$. Then we partition the sequence into two sequences, $(2, 5, 2, 0)$ and $(1, 7, 4)$. Then we recursively find the balancing points for them. We can find $5$ (index $1$) for the partition on the left, because $2 \times 1 = 2 \times 1 + 0 \times 2$, but no balancing point on the right. After that the recursion stops and we link the nodes in each partition from right to left since we cannot find a balancing point for the new partitions or the length is less than $3$.

p10192

Note that if the seesaw has balancing point, then there is only one balancing point since there is only one center of mass for every seesaw. In addition, the torque needs to be stored in a “long long int” to prevent integer overflow. All pointers must be set to NULL if they are not used to link the seesaws together.

Please construct the seesaw chandelier tree in the function, Node *ConstructTree( … ), then return the pointer of the root. The main program and the prototype of function ConstructTree are as followed:

construct.h

123456789#ifndef CONSTRUCT#define CONSTRUCTtypedef struct Node{    int value;    struct Node *left, *right;} Node; Node *ConstructTree(int T[], int N);#endif


main.c

12345678910111213141516171819202122232425262728#include <stdio.h>#include "construct.h" #define MAXN 16384 int T[MAXN]; void PrintTree( Node *root ){    if (root == NULL)        return;     printf("%d\n", root->value);    PrintTree(root->left);    PrintTree(root->right);    return;} int main(){    int N;    scanf("%d", &N);    for (int i=0; i<N; i++)        scanf("%d", &T[i]);     Node *tree = ConstructTree( T, N );     PrintTree( tree );    return 0;}


## Compile

gcc -std=c99 -O2 -c construct.c -lmgcc -std=c99 -O2 -c main.c -lmgcc -std=c99 -O2 main.o construct.o -lm


• 10 points: It is guaranteed you can only find the balancing point at the top level, and none at the second level.
• 20 points: It is guaranteed you can only find the balancing point at the top level and the second level, but none at the third level.
• 70 points: It is guaranteed you can find the balancing point at the top, but nothing is guaranteed at other levels.

## Input Format

The input contains only one test case. The first line contains one integers $n$, representing the number of sequence. The second line contains $n$ integers, $w_{1}$, $w_{2}$, … , $w_{n}$, representing the weights of index.

• $0\lt n\lt 16384$

## Output Format

Print all the values of nodes in the tree by pre-ordering, and each node in a line.

## Sample Input 0

31 0 0


## Sample Output 0

100


## Sample Input 1

42 1 5 5


## Sample Output 1

5125


## Sample Input 2

92 5 2 0 7 1 1 4 3


## Sample Output 2

752024113


## Sample Input 3

82 5 2 0 7 1 7 4


## Sample Output 3

75202471