#FT0000075. Phân phối phần tử vào hai dãy II (Distribute Elements Into Two Arrays II)
Bài tập thuộc lộ trình
Mục lục đề ⌄
Phân phối phần tử vào hai dãy II (Distribute Elements Into Two Arrays II)
Distribute Elements Into Two Arrays II
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Given integers , distribute them, in order, into two initially empty arrays and using exactly operations, each appending one item to one array.
First append to , then append to . For each later value (), let be the number of elements in strictly greater than . Append to the array with larger ; if tied, to the shorter array; if still tied, to .
After all insertions, concatenate followed by , preserving the internal order of both. Output the resulting array of integers.
Input
The first line contains integer .
The second line contains integers in distribution order.
Output
Print integers on one line: all elements of followed by all elements of , separated by spaces.
Subtasks
-
Subtask 1 (20 points): ; for every .
-
Subtask 2 (30 points): ; for every .
-
Subtask 3 (50 points): ; for every .
Examples
Input
5
5 14 3 1 2
Output
5 3 1 2 14
Explanation
The first two operations produce and .
- For : and . Both arrays have equal length, so append to .
- For : from , versus . Append to .
- For : from , versus . Append to .
Finally . Concatenation gives 5 3 1 2 14.