日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

CF1189B Number Circle(数字圈)

發(fā)布時間:2024/3/24 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CF1189B Number Circle(数字圈) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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個數(shù),a1...an。是否存在一種排列方式使得每個數(shù)比與他相鄰的兩個數(shù)的和小?

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.

比如說,對于這個數(shù)列,左側(cè)排列是可以的,右側(cè)排列是不行的

Input

The first line contains a single integer?nn?(3≤n≤1053≤n≤105)?— the number of numbers.

第一行包含一個整數(shù)n——數(shù)字的個數(shù)

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個整數(shù)——這些數(shù)字。給定的數(shù)字并不是絕對不同的(也就是說相同的數(shù)字也是可以的)

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 3

output

Copy

YES 4 2 3

input

Copy

5 1 2 3 4 4

output

Copy

YES 4 4 2 1 3

input

Copy

3 13 8 5

output

Copy

NO

input

Copy

4 1 10 100 1000

output

Copy

NO

Note

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)放在左側(cè),把b(n-2)放在右側(cè),由于數(shù)列有序,所以b(n-3)+b(n-1)>b(n-2)成立,以此類推到最后一個元素。b(n)b(n-2)? 1,由于b(n)顯然大于b(n-2),所以這一小節(jié)依然成立。所以全體數(shù)列符合題意。

所以思路如下:

  • 對數(shù)組進行排序,并進行判斷。
  • //#include<pch.h> #include <iostream> #include <cstdio> #include <bits/stdc++.h> #include <map> #include <algorithm> #include <stack> #include <iomanip> #include <cstring> #include <cmath> #define DETERMINATION main #define lldin(a) scanf("%lld", &a) #define println(a) printf("%lld\n", a) #define reset(a, b) memset(a, b, sizeof(a)) const int INF = 0x3f3f3f3f; using namespace std; const double PI = acos(-1); typedef long long ll; typedef unsigned long long ull; typedef long double ld; const int mod = 1000000007; const int tool_const = 19991126; const int tool_const2 = 33; inline ll lldcin() {ll tmp = 0, si = 1;char c;c = getchar();while (c > '9' || c < '0'){if (c == '-')si = -1;c = getchar();}while (c >= '0' && c <= '9'){tmp = tmp * 10 + c - '0';c = getchar();}return si * tmp; } ///Untersee Boot IXD2(1942) /**Although there will be many obstructs ahead, the desire for victory still fills you with determination..**/ /**Last Remote**/ const int MAX = 3e5; ll a[MAX]; int DETERMINATION() {ll n;cin >> n;for (int i = 1; i <= n; i++)cin >> a[i];sort(a + 1, a + n + 1);if (a[n - 1] + a[n - 2] <= a[n])//不滿足條件{cout << "NO" << endl;}else{cout << "YES" << endl;for (int i = 1; i <= n - 3; i++)cout << a[i] << " ";cout << a[n - 1] << " " << a[n] << " " << a[n-2] << endl;} return 0; }

    ?

    ?

    總結(jié)

    以上是生活随笔為你收集整理的CF1189B Number Circle(数字圈)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。