A14290 | Challenging Cliffs
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are a game designer and want to make an obstacle course. The player will walk from left to right. You have $n$ heights of mountains already selected and want to arrange them so that the absolute difference of the heights of the first and last mountains is as small as possible.
In addition, you want to make the game difficult, and since walking uphill or flat is harder than walking downhill, the difficulty of the level will be the number of mountains $i$ ( $1 \leq i < n$ ) such that $h_i \leq h_{i+1}$ where $h_i$ is the height of the $i$ -th mountain. You don't want to waste any of the mountains you modelled, so you have to use all of them.
From all the arrangements that minimize $|h_1-h_n|$ , find one that is the most difficult. If there are multiple orders that satisfy these requirements, you may find any.
In addition, you want to make the game difficult, and since walking uphill or flat is harder than walking downhill, the difficulty of the level will be the number of mountains $i$ ( $1 \leq i < n$ ) such that $h_i \leq h_{i+1}$ where $h_i$ is the height of the $i$ -th mountain. You don't want to waste any of the mountains you modelled, so you have to use all of them.
From all the arrangements that minimize $|h_1-h_n|$ , find one that is the most difficult. If there are multiple orders that satisfy these requirements, you may find any.
输入格式
The first line will contain a single integer $t$ ( $1 \leq t \leq 100$ ) — the number of test cases. Then $t$ test cases follow.
The first line of each test case contains a single integer $n$ ( $2 \leq n \leq 2 \cdot 10^5$ ) — the number of mountains.
The second line of each test case contains $n$ integers $h_1,\ldots,h_n$ ( $1 \leq h_i \leq 10^9$ ), where $h_i$ is the height of the $i$ -th mountain.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ .
The first line of each test case contains a single integer $n$ ( $2 \leq n \leq 2 \cdot 10^5$ ) — the number of mountains.
The second line of each test case contains $n$ integers $h_1,\ldots,h_n$ ( $1 \leq h_i \leq 10^9$ ), where $h_i$ is the height of the $i$ -th mountain.
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$ .
输出格式
For each test case, output $n$ integers — the given heights in an order that maximizes the difficulty score among all orders that minimize $|h_1-h_n|$ .
If there are multiple orders that satisfy these requirements, you may output any.
If there are multiple orders that satisfy these requirements, you may output any.
输入输出样例
输入 #1
2 4 4 2 1 2 2 3 1
输出 #1
2 4 1 2 1 3
In the first test case:
The player begins at height $2$ , next going up to height $4$ increasing the difficulty by $1$ . After that he will go down to height $1$ and the difficulty doesn't change because he is going downhill. Finally the player will go up to height $2$ and the difficulty will increase by $1$ . The absolute difference between the starting height and the end height is equal to $0$ and it's minimal. The difficulty is maximal.
In the second test case:
The player begins at height $1$ , next going up to height $3$ increasing the difficulty by $1$ . The absolute difference between the starting height and the end height is equal to $2$ and it's minimal as they are the only heights. The difficulty is maximal.
The player begins at height $2$ , next going up to height $4$ increasing the difficulty by $1$ . After that he will go down to height $1$ and the difficulty doesn't change because he is going downhill. Finally the player will go up to height $2$ and the difficulty will increase by $1$ . The absolute difference between the starting height and the end height is equal to $0$ and it's minimal. The difficulty is maximal.
In the second test case:
The player begins at height $1$ , next going up to height $3$ increasing the difficulty by $1$ . The absolute difference between the starting height and the end height is equal to $2$ and it's minimal as they are the only heights. The difficulty is maximal.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted