CF1189 B. Number Circle

题目传送门:https://codeforces.com/problemset/problem/1189/B

题目大意:
给定一串长度为(n)的序列(A(1leqslant A_ileqslant 10^9)),问能否将序列(A)任意排序后,使得(forall iin[1,n]),都有(A_i<A_{i-1}+A_{i+1})

由于序列(A)是环形构造,故我们认为(A_0=A_{n},A_{n+1}=A_{1})


给序列(A)排序(降序),在任意一个位置放置(A_1)后,我们在其左边依次放置(A_3,A_5,...A_{2k+1}),在其右边依次放置(A_{2},A_{4},...,A_{2k}),由于序列是降序排列,故(A_{2k}<A_{2k-2}+alpha,A_{2k+1}<A_{2k-1}+alpha)(alpha)为任意非零整数

故我们仅需判断(A_1<A_2+A_3)是否成立即可

/*program from Wolfycz*/
#include<map>
#include<cmath>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#define Fi first
#define Se second
#define ll_inf 1e18
#define MK make_pair
#define sqr(x) ((x)*(x))
#define pii pair<int,int>
#define int_inf 0x7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline char gc(){
	static char buf[1000000],*p1=buf,*p2=buf;
	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
}
template<typename T>inline T frd(T x){
	int f=1; char ch=gc();
	for (;ch<'0'||ch>'9';ch=gc())	if (ch=='-')    f=-1;
	for (;ch>='0'&&ch<='9';ch=gc())	x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}
template<typename T>inline T read(T x){
	int f=1; char ch=getchar();
	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')	f=-1;
	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<1)+(x<<3)+ch-'0';
	return x*f;
}
inline void print(int x){
	if (x<0)	putchar('-'),x=-x;
	if (x>9)	print(x/10);
	putchar(x%10+'0');
}
const int N=1e5;
int A[N+10],B[N+10];
int main(){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	int n=read(0);
	for (int i=1;i<=n;i++)	A[i]=read(0);
	sort(A+1,A+1+n);
	reverse(A+1,A+1+n);
	if (A[1]>=A[2]+A[3]){
		printf("NO
");
		return 0;
	}
	for (int i=1;i<=n;i+=2)	B[(i+1)>>1]=A[i];
	
	for (int i=n,j=2;j<=n;i--,j+=2)	B[i]=A[j];
	
	printf("YES
");
	for (int i=1;i<=n;i++)	printf("%d%c",B[i],i==n?'
':' ');
	return 0;
}
作者:Wolfycz
本文版权归作者和博客园共有,欢迎转载,但必须在文章开头注明原文出处,否则保留追究法律责任的权利
原文地址:https://www.cnblogs.com/Wolfycz/p/14982484.html