Codeforces Round 1061 (Div. 2)
E
Best Time to Buy and Sell Stock
Codeforces
2500
原题
← DF1 →
翻译中…

The beauty of an array $b$ of length $m$ (with $m \ge 2$) is defined as the largest value of $b_j - b_i$ over all pairs of indices $i$ and $j$ such that $1\le i \lt j\le m$. More formally, it is equal to $\max\limits_{1\le i \lt j\le m} (b_j - b_i)$. Note that the beauty might be negative if the array is strictly decreasing.

翻译中…

Hao and Alex play a turn-based game on an array $a$ of length $n$. Initially, all elements of the array are unlocked. The players take turns alternately, with Hao going first.

翻译中…
  • On Hao's turn, he selects one unlocked element from array $a$ and removes it.
  • On Alex's turn, he selects one unlocked element from array $a$ and locks it (so it can no longer be removed by Hao).
翻译中…

The game continues until all elements of $a$ are either locked or removed. It can be proven that the game lasts exactly $n$ turns, and exactly $\left\lfloor \frac{n}{2} \right\rfloor$ elements will remain locked in array $a$ at the end.

翻译中…

Hao wants to minimize the beauty of the final array of locked elements, while Alex wants to maximize it. Determine the beauty of the final array if both players play optimally.

翻译中…

Input

翻译中…

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.

翻译中…

The first line of each test case contains a single integer $n$ ($4\le n\le 10^5$) — the size of the array $a$.

翻译中…

The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le 10^9$) — the elements of the array $a$.

翻译中…

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

翻译中…

Output

翻译中…

For each test case, output a single integer representing the beauty of the final array if both Hao and Alex play optimally.

翻译中…

Note

翻译中…

In the first test case, the game may proceed as follows. Bolded elements are locked by Alex:

翻译中…
  • Turn 1 (Hao): Remove element $1$ (at position $2$), resulting in $[5, 2, 3, 4]$.
  • Turn 2 (Alex): Lock element $3$ (at position $3$), resulting in $[5, 2, \mathbf{3}, 4]$.
  • Turn 3 (Hao): Remove element $2$ (at position $2$), resulting in $[5, \mathbf{3}, 4]$.
  • Turn 4 (Alex): Lock element $4$ (at position $3$), resulting in $[5, \mathbf{3}, \mathbf{4}]$.
  • Turn 5 (Hao): Remove element $5$ (at position $1$), resulting in $[\mathbf{3}, \mathbf{4}]$.
翻译中…

The beauty of the final array $b = [3, 4]$ is equal to $b_2 - b_1 = 4 - 3 = 1$.

翻译中…

In the second test case, note that the answer might be negative:

翻译中…
  • Turn 1 (Hao): Remove element $2$ (at position $3$), resulting in $[3, 1, 1]$.
  • Turn 2 (Alex): Lock element $1$ (at position $2$), resulting in $[3, \textbf{1}, 1]$.
  • Turn 3 (Hao): Remove element $1$ (at position $3$), resulting in $[3, \textbf{1}]$.
  • Turn 4 (Alex): Lock element $3$ (at position $1$), resulting in $[\textbf{3}, \textbf{1}]$.
翻译中…

The beauty of the final array $b = [3, 1]$ is equal to $b_2 - b_1 = 1 - 3 = -2$.

翻译中…

In the third test case, one possible final array (assuming optimal play by both players) is $b = [3, 5, 8, 5, 1]$. The beauty, which is the largest difference $b_j - b_i$ over all pairs of indices $i$ and $j$ such that $1\le i \lt j\le 5$, is equal to $b_3 - b_1 = 8 - 3 = 5$.

翻译中…

样例

Input #1
6
5
5 1 2 3 4
4
3 1 2 1
10
7 1 3 5 8 2 8 3 5 1
6
1 1 4 5 1 4
9
9 9 8 2 4 4 3 5 3
4
1000000000 1 2 3
Output #1
1
-2
5
3
1
-999999998
InputCopy OutputCopy
题目信息
题号
2156E
时间限制
4 s
内存限制
256 MB
标签
二分
暴力
数据结构
动态规划
博弈论
图论
贪心

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。