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点 ...
随机推荐
- Python学习——1113
在命令界面直接输入python,进入交互模式,相当于启动了Python解释器,需要一行一行的输入,输入一行,执行一行. 在命令界面直接运行.py文件,相当于直接启动了Python解释器,一次性把.py ...
- Linux 备份
备份之前的准备工作 安装常用的软件 常用软件的安装,见我另一篇blog Ubuntu 16.04 安装札记 的第四部分. 清理系统中没用的垃圾 至于垃圾清理,主要清理对象有 sudo rm -r ~/ ...
- [Java] 新手快速就业需要掌握的知识点
目的:主要是分享下日常工作中使用到的技术点,根据二八定律快速掌握使用知识点,先就业再沉淀去积累经验.(个人建议仅供参考) 背景:目前一般来说,都是前后端分离.你只需要提供接口给前端,他来处理就可以了, ...
- Python 模块(二)
1 logging 模块 logging有两种的配置的方式,configure.logger 1.1 config方式 import logging ''' 日志的配置:config模式 只能选择在屏 ...
- 字符编码:Unicode和UTF-8的关系
今天中午,我突然想搞清楚Unicode和UTF-8之间的关系,于是就开始在网上查资料. 结果,这个问题比我想象的复杂,从午饭后一直看到晚上9点,才算初步搞清楚. 下面就是我的笔记,主要用来整理自己的思 ...
- javaweb基础(5)_servlet原理
一.Servlet简介 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发一个Java程序向 ...
- 如何理解JavaScript中的this关键字
前言 王福朋老师的 JavaScript原型和闭包系列 文章看了不下三遍了,最为一个初学者,每次看的时候都会有一种 "大彻大悟" 的感觉,而看完之后却总是一脸懵逼.原型与闭包 可以 ...
- 安全和加密——openssl及自建CA
一.对称加密算法 对称加密:加密和解密使用共用一个秘钥 特点 加密.解密使用同一个秘钥,效率高: 将原始数据分割成固定大小的块,逐个进行加密 缺点 密钥过多,密钥需要分发 数据来源无法确认 1. 使用 ...
- CentOS 7 忘记root密码解决方法
CentOS 7 root密码的重置方式和CentOS 6完全不一样,CentOS 7与之前的版本6变化还是比较大的,以进入单用户模式修改root密码为例: 1.重启机器,进入grub菜单的时候按e ...
- 不使用脚手架的 vue 应用
工作中的项目不止有页面繁多的模块化项目,还会只有一两个页面的类似于填写信息参与活动的活动页.这个时候,就可以回归以前的三剑客模式,在 index.html 里引用 vue.js 进行开发. 关键点: ...