codeforces--652B z-sort(sorting)

来源:互联网 发布:mac如何连接远程服务 编辑:程序博客网 时间:2024/06/09 20:27

传送门

题解

z-sorted sequence 有如下特点:
a1a2a3a4a5a6a7...
可以看到,偶数位置上的数总是不小于两边奇数位置上的数,排序后按此规则填即可。

#include <bits/stdc++.h>using namespace std;const int maxn = 1005;int   a[maxn], ans[maxn];int main(){    int n;    while(cin >> n)    {        for(int i = 0; i < n; ++i) cin >> a[i];        sort(a, a + n);        int j = -2;        for(int i = 0; i <= n / 2 && j < n; ++i) ans[j += 2] = a[i];  //odd        j = -1;        for(int i = n - 1; i >= n / 2 && j < n; --i) ans[j += 2] = a[i]; //even        for(int i = 0; i < n; ++i)            cout << ans[i] << " ";        cout << endl;    }    return 0;}
0 0
原创粉丝点击