Educational Codeforces Round 10——B. z-sort
1 second
256 megabytes
standard input
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?
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.
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".
4
1 2 2 1
1 2 1 2
5
1 3 2 2 5
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的更多相关文章
- Educational Codeforces Round 10
A:Gabriel and Caterpillar 题意:蜗牛爬树问题:值得一提的是在第n天如果恰好在天黑时爬到END,则恰好整除,不用再+1: day = (End - Begin - day0)/ ...
- 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 ...
- Educational Codeforces Round 10 D. Nested Segments
D. Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Educational Codeforces Round 10 D. Nested Segments (树状数组)
题目链接:http://codeforces.com/problemset/problem/652/D 给你n个不同的区间,L或者R不会出现相同的数字,问你每一个区间包含多少个区间. 我是先把每个区间 ...
- CF Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组
题目链接:http://codeforces.com/problemset/problem/652/D 大意:给若干个线段,保证线段端点不重合,问每个线段内部包含了多少个线段. 方法是对所有线段的端点 ...
- Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化
D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...
- Educational Codeforces Round 10 A. Gabriel and Caterpillar 模拟
A. Gabriel and Caterpillar 题目连接: http://www.codeforces.com/contest/652/problem/A Description The 9-t ...
- 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 ...
- Educational Codeforces Round 10 E - Pursuit For Artifacts (强联通缩点 + 回溯)
题目链接:http://codeforces.com/contest/652/problem/E 给你n个点m个边,x和y双向连接,要是z是1表示这条边上有宝藏,0则没有,最后给你起点和终点,问你要是 ...
- Educational Codeforces Round 10 A B题、
A. Gabriel and Caterpillar 题意: 就是说 一个小孩子去观察毛毛虫从 h1的地方爬到h2的地方.毛毛虫从10点爬到22点.每小时爬的距离是a, 晚上22点到第二天早上10点 ...
随机推荐
- 给广大码农分享福利:一个业界良心的github仓库,中文计算机资料
我今天查资料时无意发现的,https://github.com/CyC2018/CS-Notes 这个仓库包含了下列几个维度的计算机学习资料: 深受国内程序员喜爱,已经有超过3万多star了. 1. ...
- VC 对话框设置背景颜色和图片
改变背景颜色,有两种方法: 1.在app的初始化函数中调用:void SetDialogBkColor( COLORREF clrCtlBk = RGB(192, 192, 192), COLORRE ...
- codeforce Gym 100342J Triatrip (bitset)
傻逼题,但是为什么别人的O(n^3)不会T?只是因为用了bitset优化... 附上一张bitset基本操作的表 #include<bits/stdc++.h> using namespa ...
- lca(最近公共祖先(离线))
转自大佬博客 : https://www.cnblogs.com/JVxie/p/4854719.html LCA 最近公共祖先 Tarjan(离线)算法的基本思路及其算法实现 首先是最近公共祖先 ...
- 20181111 计时器影响DOM点击事件的逻辑
今天在群里看见一个人在问"点击按钮使图片产生旋转为什么要使用计时器来实现",我自己操作了一遍她的代码才发现里面的逻辑实现很有意思,所以写出来分享一下. 她的代码是这样写的: < ...
- JS正则表达式学习总结
JS正则:java RegExp对象,它是对字符串执行模式匹配的强大工具.运用最多的就是在输入处验证输入的字符串是否合法,指定用户输入字符串的格式. 定义方法: 1:直接量语法:var re=/pat ...
- 【linux】【指令集】查看是否打开selinux
> getenforce selinux相关原理资料参考 <鸟哥的linux私房菜> http://cn.linux.vbird.org/linux_server/0210netw ...
- 【php】instanceof
instanceof 的使用还有一些陷阱必须了解.在 PHP 5.1.0 之前,如果要检查的类名称不存在,instanceof 会调用__autoload().另外,如果该类没有被装载则会产生一个致命 ...
- 【nginx】 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream
2013/10/22 20:05:49 [error] 12691#0: *6 FastCGI sent in stderr: "Primary script unknown" w ...
- python爬虫基础16-cookie在爬虫中的应用
Cookie的Python爬虫应用 Cookie是什么 Cookie,有时也用其复数形式 Cookies,英文是饼干的意思.指某些网站为了辨别用户身份.进行 session 跟踪而储存在用户本地终端上 ...