Codeforces 1189B Number Circle

题目链接:http://codeforces.com/problemset/problem/1189/B


AC代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int a[maxn];
int b[maxn];
int c[maxn];
bool check(int i)
{
    if(a[i] < a[i-1]+a[i+1]) return true;
    else return false;
}
int main()
{
    int n;
    cin >> n;
    for(int i = 0;i < n;i++)
    {
        cin >> a[i];
    }
    sort(a,a+n);
    int b1 = 0,c1 = 0;
    for(int i = 0;i < n;i++)
    {
        if(i%2 == 0) c[c1++] = a[i];
        else b[b1++] = a[i];
    }
    for(int i = 0;i < c1;i++)
    {
        a[i] = c[i];
    }
    for(int i = c1;i < n;i++)
    {
        a[i] = b[--b1];
    }
    bool flag = true;
    for(int i = 1;i < n-1;i++)
    {
        if(!check(i)) flag = false;
    }
    if(!flag || a[n-1] >= a[0] + a[n-2] || a[0] >= a[n-1] + a[1]) cout << "NO";
    else
    {
        cout << "YES" << endl;
        for(int i = 0;i < n;i++)
        {
            cout << a[i] << " ";
        }
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Carered/p/11165875.html