Codeforces Round #541 (Div. 2) C.Birthday

链接:https://codeforces.com/contest/1131/problem/C

题意:

求给的n个数,相邻差值最小的排列方式。1-n相邻。

思路:

sort后隔一个取一个,取到底后再反着取剩下的。

代码:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

const int MAXN = 100 + 10;
int a[MAXN];

int main()
{
    int n;
    cin >> n;
    for (int i = 1;i <= n;i++)
        cin >> a[i];
    sort(a + 1, a + 1 + n);
    vector<int> res;
    vector<int> tmp;
    for (int i = 1;i <= n;i += 2)
        res.push_back(a[i]);
    for (int i = 2;i <= n;i += 2)
        tmp.push_back(a[i]);
    reverse(tmp.begin(), tmp.end());
    for (auto x : tmp)
        res.push_back(x);
    for (auto x : res)
        cout << x << ' ';

    return 0;
}

  

posted @ 2019-02-24 00:48  YDDDD  阅读(190)  评论(0编辑  收藏  举报