zoukankan      html  css  js  c++  java
  • 排序——B. z-sort

    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 arez-sorted if two conditions hold:

    1. ai ≥ ai - 1 for all even i,
    2. 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

     题意:给出一个Z-sort的定义,就是对于一个序列它满足ai ≥ ai - 1 for all even i          ai ≤ ai - 1 for all odd i > 1      对于从1开始编号的序列,偶数位比他的前一个要大,奇数为要比他的前一位要小,也就是说这个序列是一大一下一大一小这样排序的。

    题解:把这个序列先按照从小到大排序,然后将这个序列分为两半,后面的一半插入前面的 一半,这样就可以尽量保证是一大一小的,因为后面的数一定是大于或者等于前面数的。最后再按照定义判断一下,是否得到了正确的数列,没有则输出Impossible。

     1 /*B*/
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 
     7 const int maxn=1000+100;
     8 
     9 int main()
    10 {
    11     int n;
    12     int a[maxn];
    13     while(scanf("%d",&n)!=EOF)
    14     {
    15         for(int i=0;i<n;i++)
    16             scanf("%d",&a[i]);
    17         sort(a,a+n);
    18         int sa[maxn];
    19         memset(sa,0,sizeof(sa));
    20         int i=0,j=0;
    21         while(1)
    22         {
    23             sa[i]=a[j];
    24             i+=2;j++;
    25             if(j>=((n+1)/2))
    26                 break;
    27         }
    28         i=1;j=j;
    29         while(1)
    30         {
    31             sa[i]=a[j];
    32             i+=2;j++;
    33             if(j>=n)
    34                 break;
    35         }
    36         for(i=n;i>=1;i--)
    37             sa[i]=sa[i-1];
    38         int flag=1;
    39         i=2;
    40         while(1)
    41         {
    42             if(i>n)
    43                 break;
    44             if(sa[i]<sa[i-1])
    45             {
    46                 flag=0;
    47                 break;
    48             }
    49             i+=2;
    50         }
    51         i=3;
    52         while(1)
    53         {
    54             if(i>n)
    55                 break;
    56             if(sa[i]>sa[i-1])
    57             {
    58                 flag=0;
    59                 break;
    60             }
    61             i+=2;
    62         }
    63         if(flag==1)
    64         {
    65             for(i=1;i<=n;i++)
    66                 printf("%d ",sa[i]);
    67             printf("
    ");
    68         }
    69         else
    70             printf("Impossible
    ");
    71     }
    72     return 0;
    73 }
  • 相关阅读:
    python_levenshtein 的安装和使用
    接口测试困难
    pycharm修改windows的IP
    Excel读取,修改,新建
    appium混合应用的处理
    冒泡排序
    选择排序
    插入排序
    python中两种退出方式os._exit(),sys.exit()
    二分查找
  • 原文地址:https://www.cnblogs.com/yepiaoling/p/5330263.html
Copyright © 2011-2022 走看看