Upload code_segments/segment_325.txt with huggingface_hub
Browse files
code_segments/segment_325.txt
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
You are given $n$ arrays $a_1$, $\ldots$, $a_n$. The length of each array is two. Thus, $a_i = [a_{i, 1}, a_{i, 2}]$. You need to concatenate the arrays into a single array of length $2n$ such that the number of inversions$^{\dagger}$ in the resulting array is minimized. Note that you do not need to count the actual number of inversions.
|
| 2 |
+
|
| 3 |
+
More formally, you need to choose a permutation$^{\ddagger}$ $p$ of length $n$, so that the array $b = [a_{p_1,1}, a_{p_1,2}, a_{p_2, 1}, a_{p_2, 2}, \ldots, a_{p_n,1}, a_{p_n,2}]$ contains as few inversions as possible.
|
| 4 |
+
|
| 5 |
+
$^{\dagger}$The number of inversions in an array $c$ is the number of pairs of indices $i$ and $j$ such that $i < j$ and $c_i > c_j$.
|
| 6 |
+
|
| 7 |
+
$^{\ddagger}$A permutation of length $n$ is an array consisting of $n$ distinct integers from $1$ to $n$ in arbitrary order. For example, $[2,3,1,5,4]$ is a permutation, but $[1,2,2]$ is not a permutation ($2$ appears twice in the array), and $[1,3,4]$ is also not a permutation ($n=3$ but there is $4$ in the array).
|
| 8 |
+
|
| 9 |
+
Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The description of the test cases follows.
|
| 10 |
+
|
| 11 |
+
The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the number of arrays.
|
| 12 |
+
|
| 13 |
+
Each of the following $n$ lines contains two integers $a_{i,1}$ and $a_{i,2}$ ($1 \le a_{i,j} \le 10^9$) — the elements of the $i$-th array.
|
| 14 |
+
|
| 15 |
+
It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.
|
| 16 |
+
|
| 17 |
+
For each test case, output $2n$ integers — the elements of the array you obtained. If there are multiple solutions, output any of them.
|
| 18 |
+
|
| 19 |
+
In the first test case, we concatenated the arrays in the order $2, 1$. Let's consider the inversions in the resulting array $b = [2, 3, 1, 4]$:
|
| 20 |
+
|
| 21 |
+
* $i = 1$, $j = 3$, since $b_1 = 2 > 1 = b_3$; * $i = 2$, $j = 3$, since $b_2 = 3 > 1 = b_3$.
|
| 22 |
+
|
| 23 |
+
Thus, the number of inversions is $2$. It can be proven that this is the minimum possible number of inversions.
|
| 24 |
+
|
| 25 |
+
In the second
|