Description
Input
Output
Sample Input
2
3
1 2 3
5
100 350 200 101 401
Sample Output
1 3 2 100 200 101 401 350 思路:因为要求字典序最小,所以排序后小的尽量往前面放,排序完后,我们发现前三个 一定是新序列的前三个,然后之后的就交替插入就行了#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 50005; int arr[MAXN]; int brr[MAXN]; int N; int main(){ int t; scanf("%d",&t); while (t--){ scanf("%d",&N); for (int i = 1; i <= N; i++) scanf("%d",&arr[i]); sort(arr+1,arr+1+N); memset(brr,0,sizeof(brr)); brr[1] = arr[1],brr[3] = arr[2],brr[2] = arr[3]; for (int i = 4; i <= N; i+=2){ brr[i] = arr[i+1]; brr[i+1] = arr[i]; } for (int i = 1; i <= N-1; i++) printf("%d ",brr[i]); printf("%d\n",brr[N]); } return 0; }