显然可以发现:
我们sort之后,把奇数位的先按顺序拿出来,然后再把偶数位的按照反顺序拿出来,这样就可以保证答案最小。
代码:
/* code by: zstu wxk time: 2019/02/24 */ #include<bits/stdc++.h> using namespace std; #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout); #define LL long long #define ULL unsigned LL #define fi first #define se second #define pb push_back #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define lch(x) tr[x].son[0] #define rch(x) tr[x].son[1] #define max3(a,b,c) max(a,max(b,c)) #define min3(a,b,c) min(a,min(b,c)) typedef pair<int,int> pll; const int inf = 0x3f3f3f3f; const int _inf = 0xc0c0c0c0; const LL INF = 0x3f3f3f3f3f3f3f3f; const LL _INF = 0xc0c0c0c0c0c0c0c0; const LL mod = (int)1e9+7; const int N = 1e5 + 100; int n; int a[N]; vector<int> vc1, vc2; void Ac(){ for(int i = 1; i <= n; ++i) scanf("%d", &a[i]); sort(a+1, a+1+n); for(int i = 1; i <= n; i += 2) vc1.pb(a[i]); for(int i = 2; i <= n; i += 2) vc2.pb(a[i]); reverse(vc2.begin(), vc2.end()); for(int x : vc1) printf("%d ", x); for(int x : vc2) printf("%d ", x); puts(""); return ; } int main(){ while(~scanf("%d", &n)){ Ac(); } return 0; }View Code