CF1189B Number Circle(数字圈)
You are given?nn?numbers?a1,a2,…,ana1,a2,…,an. 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?
給定了n個數,a1...an。是否存在一種排列方式使得每個數比與他相鄰的兩個數的和小?
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≥4+1?and?8>1+68>1+6.
比如說,對于這個數列,左側排列是可以的,右側排列是不行的
Input
The first line contains a single integer?nn?(3≤n≤1053≤n≤105)?— the number of numbers.
第一行包含一個整數n——數字的個數
The second line contains?nn?integers?a1,a2,…,ana1,a2,…,an?(1≤ai≤1091≤ai≤109)?— the numbers. The given numbers are not necessarily distinct (i.e. duplicates are allowed).
第二行包含n個整數——這些數字。給定的數字并不是絕對不同的(也就是說相同的數字也是可以的)
Output
If there is no solution, output "NO" in the first line.
如果沒有答案,直接輸出NO
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.
如果有一個解決方案,在第一行輸出YES。第二行輸出任何一個可行的方案。
Examples
input
Copy
3 2 4 3output
Copy
YES 4 2 3input
Copy
5 1 2 3 4 4output
Copy
YES 4 4 2 1 3input
Copy
3 13 8 5output
Copy
NOinput
Copy
4 1 10 100 1000output
Copy
NONote
One of the possible arrangements is shown in the first example:
4<2+34<2+3;
2<4+32<4+3;
3<4+23<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≥8+5.
There is no solution in the fourth example.
這個題的核心也是一個IDEA:對于“一般的排序”(逆序或順序),這個序列早已滿足了所謂“兩邊之和大于中間”的需求。比如說有序的序列1 2 3 4 5,2比1+3小,3比2+4小。這也就是說直接對原序列進行排列就可以得到一個解決方案,但是,這還得滿足一個條件。
比如對于序列1 2 3 4 5 6,將他連成一個圈就是1 2 3 4 5 6 1,而6=1+5,并不滿足條件。換句話說,你得讓兩端的連接點和它兩端的元素滿足題設條件。我們可以假設這個連接點為任何元素。假設它是b(n)元素,并且有顯然的b(n-1)+b(n-2)>b(n),那么b(n)就可以當做一個連接點,把b(n-1)放在左側,把b(n-2)放在右側,由于數列有序,所以b(n-3)+b(n-1)>b(n-2)成立,以此類推到最后一個元素。b(n)b(n-2)? 1,由于b(n)顯然大于b(n-2),所以這一小節依然成立。所以全體數列符合題意。
所以思路如下:
?
?
總結
以上是生活随笔為你收集整理的CF1189B Number Circle(数字圈)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jquery tmpl js 模板详解
- 下一篇: 四-1,区块链共识机制---POW