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:

  1. ai ≥ ai - 1 for all even i,
  2. 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

题意:给定一个数组是否能组成一个波动序列(这名称很贴切呃)。然后突然想到了双端队列,然后又想到了据说在两端操作时间很快的list类

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<list>
using namespace std;
int pos[1009];
int main(void)
{
list<int>lst;
int n,temp,i;
while (cin>>n)
{
lst.clear();
memset(pos,0,sizeof(pos));
int cnt=0;
for (i=0; i<n; i++)
{
cin>>temp;
lst.push_back(temp);
}
lst.sort();//list的sort是自带的方法.不能用普通sort
bool ok=1;
while (!lst.empty())//从一个排好序的序列里头与尾交替地取出一个数就可以组成波动序列
{
if(lst.front()<=lst.back())
{
pos[cnt++]=lst.front();
lst.pop_front();
if(lst.empty())//每次pop都特判一下
break;
pos[cnt++]=lst.back();
lst.pop_back();
if(lst.empty())//每次pop都特判一下
break;
}
else
{
ok=0;
break;
}
}
if(!ok)
cout<<"Impossible"<<endl;
else
{
for (i=0; i<cnt; i++)
{
printf("%d%s",pos[i],i==cnt-1?"\n":" ");
}
cout<<endl;
}
}
return 0;
}

Educational Codeforces Round 10——B. z-sort的更多相关文章

  1. Educational Codeforces Round 10

    A:Gabriel and Caterpillar 题意:蜗牛爬树问题:值得一提的是在第n天如果恰好在天黑时爬到END,则恰好整除,不用再+1: day = (End - Begin - day0)/ ...

  2. Educational Codeforces Round 10 B. z-sort 构造

    B. z-sort 题目连接: http://www.codeforces.com/contest/652/problem/B Description A student of z-school fo ...

  3. Educational Codeforces Round 10 D. Nested Segments

    D. Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. Educational Codeforces Round 10 D. Nested Segments (树状数组)

    题目链接:http://codeforces.com/problemset/problem/652/D 给你n个不同的区间,L或者R不会出现相同的数字,问你每一个区间包含多少个区间. 我是先把每个区间 ...

  5. CF Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组

    题目链接:http://codeforces.com/problemset/problem/652/D 大意:给若干个线段,保证线段端点不重合,问每个线段内部包含了多少个线段. 方法是对所有线段的端点 ...

  6. Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化

    D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...

  7. Educational Codeforces Round 10 A. Gabriel and Caterpillar 模拟

    A. Gabriel and Caterpillar 题目连接: http://www.codeforces.com/contest/652/problem/A Description The 9-t ...

  8. Educational Codeforces Round 10 D. Nested Segments 【树状数组区间更新 + 离散化 + stl】

    任意门:http://codeforces.com/contest/652/problem/D D. Nested Segments time limit per test 2 seconds mem ...

  9. Educational Codeforces Round 10 E - Pursuit For Artifacts (强联通缩点 + 回溯)

    题目链接:http://codeforces.com/contest/652/problem/E 给你n个点m个边,x和y双向连接,要是z是1表示这条边上有宝藏,0则没有,最后给你起点和终点,问你要是 ...

  10. Educational Codeforces Round 10 A B题、

    A. Gabriel and Caterpillar 题意: 就是说  一个小孩子去观察毛毛虫从 h1的地方爬到h2的地方.毛毛虫从10点爬到22点.每小时爬的距离是a, 晚上22点到第二天早上10点 ...

随机推荐

  1. 关于vcpkg的一些技术细节

    1.如果你启用了vcpkg integrate install,将默认采用vcpkg里安装的源而不是nuget中的 2.一般而言xxx-uwp不能用xxx-windows代替,否则回捣乱其它包 3.卸 ...

  2. [学习总结] python语言学习总结 (二)

    1.python中的拆包 之前就只写了*可以是未知数量的参数,**可以传入未知数量命名参数.这次详细记下拆包. def f1(a, *l): print(a) # 不拆包 print(l) # 拆包 ...

  3. 2018.5.9 Oracle数据库查询命令

    0.查询所有数据(最简单,但是时间很久) select * from emp; Result: EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO ----- - ...

  4. argsort argmax

    argsort是将array排序并返回坐标值,默认是从小到大,添加负号-变成从大到小.这个函数并不改变原来的array. >>> a = np.array([1,5,3,1]) &g ...

  5. CPP-STL:vector的内存释放

    1. vector容器的内存自增长 与其他容器不同,其内存空间只会增长,不会减小.先来看看"C++ Primer"中怎么说:为了支持快速的随机访问,vector容器的元素以连续方式 ...

  6. javaweb基础(12)_session详解

    一.Session简单介绍 在WEB开发中,服务器可以为每个用户浏览器创建一个会话对象(session对象),注意:一个浏览器独占一个session对象(默认情况下).因此,在需要保存用户数据时,服务 ...

  7. LLDB详解

    LLDB的Xcode默认的调试器,它与LLVM编译器一起,带给我们更丰富的流程控制和数据检测的调试功能.平时用Xcode运行程序,实际走的都是LLDB.熟练使用LLDB,可以让你debug事半功倍 L ...

  8. json_decode()和json_encode()区别----2015-0929

    json_decode对JSON格式的字符串进行编码而json_encode对变量进行 JSON 编码,需要的朋友可以参考下   1.json_decode() json_decode (PHP 5 ...

  9. pandas关联mysql并读写数据库

    1.代码读写mysql,必须安装关联mysql的工具 操作如下命令: sudo apt-get install mysql-server mysql-clientsudo apt-get instal ...

  10. 单行代码实现xml转换成数组

    $string = '<xml> <return_code><![CDATA[SUCCESS]]></return_code> <return_m ...