A student of z-school found a kind of sorting called z-sort. The array a with n elements are z-sorted if two conditions hold:
For example the arrays [1,2,1,2] and [1,1,1,1] are z-sorted while the array [1,2,3,4] isn’t z-sorted.
Can you make the array z-sorted?
The first line contains a single integer n (1 ≤ n ≤ 1000) — the number of elements in the array a.
The second line contains n integers ai (1 ≤ ai ≤ 109) — the elements of the array a.
If it's possible to make the array a z-sorted print n space separated integers ai — the elements after z-sort. Otherwise print the only word "Impossible".
4 1 2 2 1
1 2 1 2
5 1 3 2 2 5
1 5 2 3 2
给你n个数(1 ≤ n ≤ 1000),要求你对这些数进行重排列(1 ≤ ai ≤ 10^9),要求这个位置如果为偶数的话,他就要大于他边上的两个位置(一左一右),最后输出这个序列。
思路:
我们对这些序列进行一个按从小到大进行排序,把最大的数都放在偶数位置,剩下的数放在奇数位置。
#include <map> #include <set> #include <stack> #include <queue> #include <cmath> #include <ctime> #include <vector> #include <cstdio> #include <cctype> #include <cstring> #include <cstdlib> #include <iostream> #include <algorithm> using namespace std; #define INF 0x3f3f3f3f #define inf -0x3f3f3f3f #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define mem0(a) memset(a,0,sizeof(a)) #define mem1(a) memset(a,-1,sizeof(a)) #define mem(a, b) memset(a, b, sizeof(a)) typedef long long ll; const int maxn=1010; int a[maxn],ans[maxn]; int main(){ int n; while(scanf("%d",&n)!=EOF){ for(int i=1;i<=n;i++) scanf("%d",&a[i]); sort(a+1,a+n+1); for(int i=2,j=n;i<=n;j--,i+=2) ans[i]=a[j]; for(int i=1,j=1;i<=n;j++,i+=2) ans[i]=a[j]; for(int i=1;i<=n;i++){ if(i!=n) printf("%d ",ans[i]); else printf("%d\n",ans[i]); } } return 0; }