Educational Codeforces Round 10 B. z-sort
2016-03-29 16:03
183 查看
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
给你n个数(1 ≤ n ≤ 1000),要求你对这些数进行重排列(1 ≤ ai ≤ 10^9),要求这个位置如果为偶数的话,他就要大于他边上的两个位置(一左一右),最后输出这个序列。
思路:
我们对这些序列进行一个按从小到大进行排序,把最大的数都放在偶数位置,剩下的数放在奇数位置。
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
const int maxn=1010;
int a[maxn],ans[maxn];
int main(){
int n;
while(scanf("%d",&n)!=EOF){
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1);
for(int i=2,j=n;i<=n;j--,i+=2)
ans[i]=a[j];
for(int i=1,j=1;i<=n;j++,i+=2)
ans[i]=a[j];
for(int i=1;i<=n;i++){
if(i!=n)
printf("%d ",ans[i]);
else
printf("%d\n",ans[i]);
}
}
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
给你n个数(1 ≤ n ≤ 1000),要求你对这些数进行重排列(1 ≤ ai ≤ 10^9),要求这个位置如果为偶数的话,他就要大于他边上的两个位置(一左一右),最后输出这个序列。
思路:
我们对这些序列进行一个按从小到大进行排序,把最大的数都放在偶数位置,剩下的数放在奇数位置。
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef long long ll;
const int maxn=1010;
int a[maxn],ans[maxn];
int main(){
int n;
while(scanf("%d",&n)!=EOF){
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1);
for(int i=2,j=n;i<=n;j--,i+=2)
ans[i]=a[j];
for(int i=1,j=1;i<=n;j++,i+=2)
ans[i]=a[j];
for(int i=1;i<=n;i++){
if(i!=n)
printf("%d ",ans[i]);
else
printf("%d\n",ans[i]);
}
}
return 0;
}
相关文章推荐
- 利用 Composer 一步一步构建自己的 PHP 框架(三)——设计 MVC
- nginx配置python的cgi
- 保存今天的代码
- JVM 垃圾回收
- tcping侦听网络状态
- 自定义水平进度条(颜色)
- Elasticsearch Exception:Message not fully read (response) for requestId
- JavaSe基础(6)-- 方法
- OpenStack入门 之 初步了解
- 直方图均衡化
- Android 应用程序集成FaceBook 登录及二次封装
- vs 或 Sql server2012连接Sql server时出现的问题:已成功与服务器建立连接,但在登陆过程中发生错误
- 线程平均分配
- 利用 Composer 一步一步构建自己的 PHP 框架(二)——构建路由
- git commit的--amend选项
- Linux 性能测试工具
- switch 语句
- Jfinal中的render
- java函数参数传参
- UVA 11552 Fewest Flops 【区间dp】