Description
Acmer in HDU-ACM team are ambitious, especially shǎ崽, he can spend time in Internet bar doing problems overnight. So many girls want to meet and Orz him. But Orz him is not that easy.You must solve this problem first.
The problem is :
Give you a sequence of distinct integers, choose numbers as following : first choose the biggest, then smallest, then second biggest, second smallest etc. Until all the numbers was chosen .
For example, give you 1 2 3 4 5, you should output 5 1 4 2 3
The problem is :
Give you a sequence of distinct integers, choose numbers as following : first choose the biggest, then smallest, then second biggest, second smallest etc. Until all the numbers was chosen .
For example, give you 1 2 3 4 5, you should output 5 1 4 2 3
Input
There are multiple test cases, each case begins with one integer N(1 <= N <= 10000), following N distinct integers.
Output
Output a sequence of distinct integers described above.
Sample Input
5 1 2 3 4 5
Sample Output
5 1 4 2 3
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n ;
map<int,int> p;
map<int,int>::iterator it;
int flag ;
int main()
{
while(scanf("%d",&n)==1)
{
int t;
p.erase(p.begin(),p.end());
for(int i=1;i<=n;i++)
{
scanf("%d",&t);
if((it=p.find(t)) == p.end())
p.insert(pair<int,int>(t,1));
else
it->second ++;
}
flag = 0;
for(int i=1;i<=n;i++)
{
//printf("1\n");
if(flag)
{
it = p.begin();
printf("%d",it->first);
it->second--;
if(it->second==0)
p.erase(it);
flag = 0;
}
else
{
it = p.end();
it--;
printf("%d",it->first);
it->second--;
if(it->second==0)
p.erase(it);
flag =1;
}
if(i!=n)
printf(" ");
}
printf("\n");
}
return 0;
}