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
output
input
output
题意:给你一个数组,让你构造出一个新的数组,它需要满足一下性质:
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;
}
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;
}
相关文章推荐
- Python 基础 —— str
- HDOJ 2037 今年暑假不AC
- 图片滚动
- 闭包
- 《日暮农归》
- Andfix热修复框架原理及源码解析-下篇
- 新的Calculator的规范作业
- Educational Codeforces Round 10(A)模拟
- Windows下MongoDB安装与PHP扩展
- 匹配html中a标签的内容
- select2与Bootstrap冲突解决
- 手机支付
- windows 80端口被占用的解决方法
- mybatis3 date传值注意
- LeetCode 327 Count of Range Sum(分治)
- 《秋诗之立残阳》
- [Androidmac安装android studio 启动后,创建项目显示SDK problem]
- 7 分支结构程序体验 项目
- JVM(四)--GC的三大高级算法
- 欢迎使用CSDN-markdown编辑器