【題解】Codeforces 1339B. Sorted Adjacent Differences

【題目敘述】http://codeforces.com/contest/1339/problem/B

#include <bits/stdc++.h>
using namespace std;
 
int t, n, a[100005];
 
int main(){
    cin >> t;
    while (t--){
        cin >> n;
        for (int i = 1; i <= n; i++){
            cin >> a[i];
        }
        sort(a+1, a+1+n);
        if (n % 2 == 0){
            for (int i = 0; i < n/2; i++){
                cout << a[n/2-i] << " " << a[n/2+1+i] << " ";
            }
        }
        else{
            cout << a[n/2+1] << " ";
            for (int i = 0; i < n/2; i++){
                cout << a[n/2-i] << " " << a[n/2+2+i] << " ";
            }
        }
        cout << "\n";
    }
}
分享本文 Share with friends