A12701 | Number Circle
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
You are given $n$ numbers $a_1, a_2, \ldots, a_n$ . Is it possible to arrange them in a circle in such a way that every number is strictly less than the sum of its neighbors?
For example, for the array $[1, 4, 5, 6, 7, 8]$ , the arrangement on the left is valid, while arrangement on the right is not, as $5\ge 4 + 1$ and $8> 1 + 6$ .

For example, for the array $[1, 4, 5, 6, 7, 8]$ , the arrangement on the left is valid, while arrangement on the right is not, as $5\ge 4 + 1$ and $8> 1 + 6$ .

输入格式
The first line contains a single integer $n$ ( $3\le n \le 10^5$ ) — the number of numbers.
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $1 \leq a_i \le 10^9$ ) — the numbers. The given numbers are not necessarily distinct (i.e. duplicates are allowed).
The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ ( $1 \leq a_i \le 10^9$ ) — the numbers. The given numbers are not necessarily distinct (i.e. duplicates are allowed).
输出格式
If there is no solution, output "NO" in the first line.
If there is a solution, output "YES" in the first line. In the second line output $n$ numbers — elements of the array in the order they will stay in the circle. The first and the last element you output are considered neighbors in the circle. If there are multiple solutions, output any of them. You can print the circle starting with any element.
If there is a solution, output "YES" in the first line. In the second line output $n$ numbers — elements of the array in the order they will stay in the circle. The first and the last element you output are considered neighbors in the circle. If there are multiple solutions, output any of them. You can print the circle starting with any element.
输入输出样例
输入 #1
3 2 4 3
输出 #1
YES 4 2 3
输入 #2
5 1 2 3 4 4
输出 #2
YES 4 4 2 1 3
输入 #3
3 13 8 5
输出 #3
NO
输入 #4
4 1 10 100 1000
输出 #4
NO
One of the possible arrangements is shown in the first example:
$4< 2 + 3$ ;
$2 < 4 + 3$ ;
$3< 4 + 2$ .
One of the possible arrangements is shown in the second example.
No matter how we arrange $13, 8, 5$ in a circle in the third example, $13$ will have $8$ and $5$ as neighbors, but $13\ge 8 + 5$ .
There is no solution in the fourth example.
$4< 2 + 3$ ;
$2 < 4 + 3$ ;
$3< 4 + 2$ .
One of the possible arrangements is shown in the second example.
No matter how we arrange $13, 8, 5$ in a circle in the third example, $13$ will have $8$ and $5$ as neighbors, but $13\ge 8 + 5$ .
There is no solution in the fourth example.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted