z-sort

link:https://cn.vjudge.net/contest/190132#problem/I

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".

Example

Input

4
1 2 2 1

Output

1 2 1 2

Input

5
1 3 2 2 5

Output

1 5 2 3 2

题解:
题目要求对于每个偶数位满足a[i]>=a[i+1]
每个奇数位满足a[i]<=a[i−1]
剩下的看代码
AC代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
int a[1010],b[1010],c[1010];
bool cmp(int a,int b)
{
    return a>b;
}
int main()
{
    int n,i,t,s;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
        scanf("%d",&a[i]);
    sort(a+1,a+n+1);//先从小到大排序 
    t=n & 1 ? n/2 + 1 : n/2;
    for(i=1,s=1;i<=t;i++)
    {
        b[s]=a[i];//先把 前t个数插空放入数组b 
        s+=2;
    }   
    for(s=2;i<=n;i++)
    {
        b[s]=a[i];//再把n-t个数插到刚刚留下的空中,就满足题意 
        s+=2;
    }
    for(i=1;i<=n;i++)
        printf("%d%c",b[i],i==n?'\n':' ');
return 0;   
} 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
很抱歉,作为AI语言模型,我无法直接生成文本文件数据集和编写 MapReduce 程序。但是,我可以帮你解释一下题目中提到的一些概念和要求,希望能对你有所帮助。 题目中要求生成文本文件数据集并按照要求编写 WordCount 程序,这是一个典型的 MapReduce 任务。MapReduce 是一种分布式计算框架,可以处理大规模数据集。在这个任务中,需要编写 Map 和 Reduce 函数,Map 函数将文本文件中的单词转换为键值对,Reduce 函数将相同单词的键值对进行合并,并计算出每个单词的出现次数。 在题目要求的 WordCount 程序中,需要去除所有非 a-z 与 A-Z 的字符,并转换为小写。这可以通过在 Map 函数中进行字符串处理来实现。具体来说,在 Map 函数中,可以使用正则表达式去除非 a-z 与 A-Z 的字符,并将字符串转换为小写。例如,可以使用 Java 中的 String.replaceAll() 方法来去除非 a-z 与 A-Z 的字符。 在 MapReduce 最终输出结果中,要求满足词频按照降序排列、同一频数下按照字典序降序排列。这可以通过在 Reduce 函数中进行排序来实现。具体来说,在 Reduce 函数中,可以将键值对按照值进行排序,并将结果输出到文件中。 最后,需要注意的是,题目中要求输出结果存放于 Hadoop 文件系统的 /学号-quest-1-output 目录下,其中学号需要替换为自己的学号。同时,任务命名也需要按照要求命名。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三更鬼

谢谢老板!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值