构造题
我们从中间开始取元素,然后左右来回取最近的元素,一定可以保证范围不断地变大
#include<iostream> #include<algorithm> using namespace std; const int max_n = 1e5+100; int a[max_n]; int n; int main() { ios::sync_with_stdio(0); cin>>n; for (int i=1;i<=n;++i)cin>>a[i]; sort(a+1,a+1+n); int id1 = (n+1)/2; int id2 = id1+1; for (int i=1;i<=n;++i) { if (i&1) { cout<<a[id1]<<" "; --id1; } else { cout<<a[id2]<<" "; ++id2; } } cout<<endl; }