CF1189B.Number Circle

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given nn numbers a1,a2,…,ana_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][1, 4, 5, 6, 7, 8], the arrangement on the left is valid, while arrangement on the right is not, as 5≥4+15\ge 4 + 1 and 8>1+68 \gt 1 + 6.

给你 nn 个数 a1,a2,…,ana_1, a_2, \ldots, a_n。能否将它们排成一个环,使得每个数都严格小于其两个相邻数之和?

例如,对于数组 [1,4,5,6,7,8][1, 4, 5, 6, 7, 8],左侧的排列是合法的,而右侧的排列不合法,因为 5≥4+15 \ge 4 + 1 且 8>1+68 > 1 + 6。

输入格式

The first line contains a single integer nn (3≤n≤1053\le n \le 10^5) — the number of numbers.

The second line contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1091 \leq a_i \le 10^9) — the numbers. The given numbers are not necessarily distinct (i.e. duplicates are allowed).

第一行包含一个整数 nn(3≤n≤1053\le n \le 10^5)—— 数字的个数。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤1091 \leq a_i \le 10^9)—— 这些数字。给定的数字不一定互不相同(即允许重复)。

输出格式

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 nn 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.

如果无解,在第一行输出 “NO”。

如果有解,在第一行输出 “YES”;在第二行输出 nn 个数——即数组的元素,按其在圆圈中的顺序排列。你所输出的第一个和最后一个元素在圆圈中视为相邻。若存在多个解,输出任意一个即可。你可以从任意一个元素开始输出该圆圈。

输入输出样例

  • 输入#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+34 \lt 2 + 3;

2<4+32 \lt 4 + 3;

3<4+23 \lt 4 + 2.

One of the possible arrangements is shown in the second example.

No matter how we arrange 13,8,513, 8, 5 in a circle in the third example, 1313 will have 88 and 55 as neighbors, but 13≥8+513\ge 8 + 5.

There is no solution in the fourth example.

一种可能的排列方式如第一个示例所示:

4<2+34 \lt 2 + 3;

2<4+32 \lt 4 + 3;

3<4+23 \lt 4 + 2。

一种可能的排列方式如第二个示例所示。

在第三个示例中,无论怎样将 13,8,513, 8, 5 按环形排列,1313 的两个邻数都必为 88 和 55,但 13≥8+513\ge 8 + 5。

第四个示例无解。

输入解题思路,AI测评打分。不知道怎么写?

首页