您的位置:首页 > 其它

Educational Codeforces Round 10(B)排序,构造

2016-03-26 00:45 387 查看
B. z-sort

time limit per test
1 second

memory limit per test
256 megabytes

input
standard input

output
standard output

A student of z-school found a kind of sorting called z-sort.
The array a with n elements
are z-sorted if two conditions hold:

ai ≥ ai - 1 for
all even i,

ai ≤ ai - 1 for
all odd i > 1.

For example the arrays [1,2,1,2] and [1,1,1,1] are z-sorted
while the array [1,2,3,4] isn’t z-sorted.

Can you make the array z-sorted?

Input

The first line contains a single integer n (1 ≤ n ≤ 1000)
— the number of elements in the array a.

The second line contains n integers ai (1 ≤ ai ≤ 109)
— the elements of the array a.

Output

If it's possible to make the array a z-sorted
print n space separated integers ai —
the elements after z-sort. Otherwise print the only word "Impossible".

Examples

input
4
1 2 2 1


output
1 2 1 2


input
5
1 3 2 2 5


output
1 5 2 3 2


题意:给你一个数组,让你构造出一个新的数组,它需要满足一下性质:

1:ai ≥ ai - 1 (当i为偶数时)

2:ai ≤ ai - 1(当i为奇数时)

题解:很容易想到,排序后不断的间隔取头尾的数字就好啦!这里我使用的是双端队列,比较方便一点,不容易编写错误

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
using namespace std;

#define N int(1e5)
#define inf int(0x3f3f3f3f)
#define mod int(1e9+7)
typedef long long LL;

#ifdef CDZSC
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
int a
;
int ans
;
int main()
{
#ifdef CDZSC
freopen("i.txt", "r", stdin);
//freopen("o.txt","w",stdout);
int _time_jc = clock();
#endif
int n;
while(~scanf("%d",&n))
{
deque<int>q;
int L=0,R=n-1;
int ok=1;
for(int i=0;i<n;i++)scanf("%d",&a[i]);
sort(a,a+n);
for(int i=0;i<n;i++)
{
q.push_back(a[i]);
}
int pi=1;
while(q.size()>0)
{
if(1&pi)
{
ans[pi++]=q.front();
q.pop_front();
}
else
{
ans[pi++]=q.back();
q.pop_back();
}
}
for(int i=2;i<=n;i++)//check
{
if(i&1)
{
if(ans[i]>ans[i-1])
ok=0;
}
else
{
if(ans[i]<ans[i-1])
ok=0;
}
}
if(ok)
{
for(int i=1;i<=n;i++)
{
printf("%d ",ans[i]);
}
puts("");
}
else
{
puts("Impossible");
}
}
#ifdef CDZSC
debug("time: %d\n", int(clock() - _time_jc));
#endif
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: