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. [uva816]AbbottsRevenge Abbott的复仇(经典迷宫BFS)

    这题思路就普通的BFS加上一个维度朝向,主要是要注意输入,输出,以及细节的处理 #include<cstdio> #include<cstring> #include<q ...

  2. 绘制方式和OpenGL枚举对应关系

    绘制方式和OpenGL枚举对应关系 图元类型 OpenGL枚举量 点 GL_POINTS 线 GL_LINES 条带线 GL_LINE_STRIP 循环线 GL_LINE_LOOP 独立三角形 GL_ ...

  3. iOS开发之WIFI,3G/4G两种网络同时使用技巧

    最近遇到一个比较奇葩的需求:App与硬件通过WiFi LAN通信, 同时App需要与服务器通过3G/4G WAN通信,如下图: 众所周知,手机同时打开WiFi和3G时候,会优先走WiFi.这个该如何实 ...

  4. 数据预处理之数据规约(Data Reduction)

    数据归约策略 数据仓库中往往具有海量的数据,在其上进行数据分析与挖掘需要很长的时间 数据归约 用于从源数据中得到数据集的归约表示,它小的很多,但可以产生相同的(几乎相同的)效果 数据归约策略 维归约  ...

  5. Python 基本数据类型 (二) - 字符串1

    # ----------- 首字母大写 ---------- test = "alex is a man" v = test.capitalize() print(v): Alex ...

  6. 循环字典进行操作时出现:RuntimeError: dictionary changed size during iteration的解决方案

    在做对员工信息增删改查这个作业时,有一个需求是通过用户输入的id删除用户信息.我把用户信息从文件提取出来储存在了字典里,其中key是用户id,value是用户的其他信息.在循环字典的时候,当用户id和 ...

  7. LeetCode(279)Perfect Squares

    题目 Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9 ...

  8. Linux编程中链接库的使用

    链接库本质上是一段可执行的二进制代码,可以被操作系统载入内存执行.按加载的时机不同,链接库可以分为静态链接库和动态链接库. 静态链接库:编译过程中加载进可执行文件的库(静态库省去了运行时加载的消耗,但 ...

  9. 动态规划:Codeforces Round #427 (Div. 2) C Star sky

    C. Star sky time limit per test2 seconds memory limit per test256 megabytes inputstandard input outp ...

  10. 数学基础:HUD1124-Factorial(N!末尾0的个数)

    Factorial Problem Description The most important part of a GSM network is so called Base Transceiver ...