#P2171. Hz 吐泡泡

Hz 吐泡泡

Description

One day, on a whim, Hz blew nn different bubbles to play with (guaranteed to be no duplicates). Because he still has homework to do, he asks you to help sort these bubbles into a tree and output its postorder traversal.

Specifically, Hz uses the binary search tree (BST) algorithm to insert the bubbles into the tree in the given order. Each insertion adds the new bubble as a leaf, and the in-order traversal of the whole tree remains in ascending order.

The binary tree after all insertions is the required tree.

Input Format

A total of 22 lines.

  • The first line contains 11 integer nn.
  • The second line contains nn integers, where the ii-th integer aia_i is the size of the ii-th bubble.

Output Format

A total of n+1n+1 lines.

  • The first line outputs the depth of the tree.
  • Then output nn lines, one integer per line, giving the postorder traversal of the tree.
8
1 4 3 9 10 35 2 7

deep=5
2
3
7
35
10
9
4
1

Hint

  • This problem has two subtasks. The first subtask has 1010 test points, each worth 55 points. The second subtask has 55 test points, each worth 1010 points.
  • Constraints: 1n3×1051 \le n \le 3 \times 10^5, 1ai1091 \le a_i \le 10^9, and all aia_i are distinct.

Translated by ChatGPT 5