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

本文共 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

你可能感兴趣的文章
linux中启动 java -jar 后台运行程序
查看>>
运行web项目端口占用问题
查看>>
Java Spring-IOC和DI
查看>>
【NOIP1999】【Luogu1015】回文数(高精度,模拟)
查看>>
Linux上安装Python3.5
查看>>
crt安装
查看>>
git切换分支报错:error: pathspec 'origin/XXX' did not match any file(s) known to git
查看>>
c++中static的用法详解
查看>>
转 我修改的注册表,但是程序运行起来,还是记着以前的
查看>>
图片轮播功能
查看>>
第六周小组作业:软件测试和评估
查看>>
linux Cacti监控服务器搭建
查看>>
debian(kali Linux) 安装net Core
查看>>
centos 7防火墙设置
查看>>
自定义进度条(圆形、横向进度条)
查看>>
spark-streaming-kafka采坑
查看>>
9.Mongodb与python交互
查看>>
18-[JavaScript]-函数,Object对象,定时器,正则表达式
查看>>
读取短信回执
查看>>
EF 数据初始化
查看>>