codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1189B.md (1892B)


      1 Source: https://codeforces.com/problemset/problem/1189/B
      2 
      3 B. Number Circle
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 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?
     22 
     23 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 \gt 1 + 6$.
     24 
     25  ![](https://espresso.codeforces.com/f87046d148e42905175ba4abb1b357c76f034eb7.png)
     26 
     27 Input
     28 
     29 The first line contains a single integer $n$ ($3\le n \le 10^5$) — the number of numbers.
     30 
     31 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).
     32 
     33 Output
     34 
     35 If there is no solution, output "NO" in the first line.
     36 
     37 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.
     38 
     39 Examples
     40 
     41 Input
     42 
     43 ```
     44 3
     45 2 4 3
     46 ```
     47 
     48 Output
     49 
     50 ```
     51 YES
     52 4 2 3
     53 ```
     54 
     55 Input
     56 
     57 ```
     58 5
     59 1 2 3 4 4
     60 ```
     61 
     62 Output
     63 
     64 ```
     65 YES
     66 4 4 2 1 3
     67 ```
     68 
     69 Input
     70 
     71 ```
     72 3
     73 13 8 5
     74 ```
     75 
     76 Output
     77 
     78 ```
     79 NO
     80 ```
     81 
     82 Input
     83 
     84 ```
     85 4
     86 1 10 100 1000
     87 ```
     88 
     89 Output
     90 
     91 ```
     92 NO
     93 ```
     94 
     95 Note
     96 
     97 One of the possible arrangements is shown in the first example:
     98 
     99 $4 \lt 2 + 3$;
    100 
    101 $2 \lt 4 + 3$;
    102 
    103 $3 \lt 4 + 2$.
    104 
    105 One of the possible arrangements is shown in the second example.
    106 
    107 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$.
    108 
    109 There is no solution in the fourth example.