博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #572 (Div. 2)B
阅读量:4676 次
发布时间:2019-06-09

本文共 2548 字,大约阅读时间需要 8 分钟。

B. Number Circle

题目链接:

题目:

You are given n numbers a1,a2,…,an. Is it possible to arrange them in a circle in such a way that every number isstrictlyless 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, as5≥4+1and8>1+6

 

InputThe first line contains a single integern(3≤n≤105) — the number of numbers.The second line containsnintegersa1,a2,…,an(1≤ai≤109) — the numbers. The given numbers are not necessarily distinct (i.e. duplicates are allowed).
OutputIf 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 outputn
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.
Examples
Input
32 4 3
Output
YES
4 2 3
Input
51 2 3 4 4
Output
YES
4 4 2 1 3
Input
313 8 5
Output
NO
Input
41 10 100 1000
Output
NO
Note
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 arrange13,8,5in a circle in the third example,13will have8and5as neighbors, but13≥8+5.There is no solution in the fourth example.
题意:给出一些数,判断这些数能否组成一个环,使得每个数严格小于相邻两个数之和,若可以,按顺序打印环中数字
思路:
先给这些数排序,建立两个数组是sh,ch,再找出最大值,最大值存入一个数组中sh,一个数组从排序后的数从后往前,每隔一个数存入该数组sh,另一个数存入另一个数组ch,再建立一个数组b,sh数组从前往后存入b,ch数组从后往前存入b,这样b中存的就是一个优化的环,只要暴力判断b中每个数是否小于相邻两个数之和即可,若不可以输出NO,可以把b数组输出.
 

 

 
#include
typedef long long ll; using namespace std; const int maxn=1e5+7; int main() { int n; while(cin>>n) { ll a[maxn]; vector
sh,ch; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); sh.push_back(a[n-1]); for(int i=n-2;i>=0;i--) { if(i%2==1) ch.push_back(a[i]); else sh.push_back(a[i]); } // cout<<"sh="<
=0;i--) { b[book++]=ch[i]; } // cout<<"b="<
=b[i-1]+b[i+1]) { flag= false; break; } } if(!flag) cout<<"NO"<

 

转载于:https://www.cnblogs.com/Vampire6/p/11142302.html

你可能感兴趣的文章
老毛桃pe安装系统
查看>>
tnsnames.ora 监听配置文件详解
查看>>
洛谷P2862 [USACO06JAN]把牛Corral the Cows
查看>>
第4.17章读书笔记
查看>>
python- 属性 静态方法,类方法
查看>>
前端面试题二
查看>>
react 单元测试 (jest+enzyme)
查看>>
HTML5网站大观:10个精美的 HTML5 企业网站欣赏
查看>>
HDU 2588 GCD
查看>>
[IDDFS+背包] 洛谷P2744 [USACO5.3]量取牛奶Milk Measuring
查看>>
关于Yaffs2在u-boot中的支持
查看>>
poj 3181 Dollar Dayz (整数划分问题---递归+DP)
查看>>
android:id="@android:id/tabhost" 、android:id="@+id/llRoot" 、android:id="@id/llRoot" 之间的区别...
查看>>
MySQL 忘记Root密码
查看>>
WPF后台自定义文字带背景的选择状态按钮
查看>>
【转自Mgen】 .NET(C#):谈谈各种结束进程的方法
查看>>
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
查看>>
用原生javascript做的一个打地鼠的小游戏
查看>>
小米手机 - Charles无法安装证书 因为无法读取证书
查看>>
android 动态壁纸开发
查看>>