zoukankan      html  css  js  c++  java
  • Educational Codeforces Round 10 B. z-sort

    time limit per test1 second
    memory limit per test256 megabytes
    inputstandard input
    outputstandard 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
    Copy
    4
    1 2 2 1
    Output
    Copy
    1 2 1 2
    Input
    Copy
    5
    1 3 2 2 5
    Output
    Copy
    1 5 2 3 2

    题解:这道看起来像个排序题,第偶数个数要>=前一个数,第奇数个数要<=前一个数,

    事实上这道题看样例会发现只要把源数列排序后前面一个,结尾一个依次输出就行,比如 13 2 2 5 ,排序后为 1 2 2 3 5,

    输出第一个 1 ,输出最后一个5,输出第二个 2 ,输出倒数第二个 3,依次类推,即可.

    #include <bits/stdc++.h>
    const int N=1e3+5;
    using namespace std;
    int a[N];
    int main(){
    	int n;
    	scanf("%d",&n);
    	for(int i=1;i<=n;i++){
    		scanf("%d",&a[i]);
    	}
    	sort(a+1,a+n+1);
    	//for(int i=1;i<=n;i++) printf("%d
    ",a[i]);
    	if(n%2==0){
    		for(int i=1;i<=n/2;i++){
    			printf("%d %d ",a[i],a[n-i+1]);
    		}
    	}
    	else{
    		for(int i=1;i<=n/2;i++){
    			printf("%d %d ",a[i],a[n-i+1]);
    		}
    		printf("%d",a[n/2+1]);
    	}
    	return 0;
    	
    }
    
  • 相关阅读:
    Oracle优化器模式不同导致索引失效
    Python补零操作
    正则表达式
    python习题(二)
    Linux常见报错及解决方法(持续更新)
    总结(三)----2020上
    总结二
    总结---持更
    python多线程实现方式,最基础的实现方式模块是什么
    python2和python3区别
  • 原文地址:https://www.cnblogs.com/-yjun/p/10424257.html
Copyright © 2011-2022 走看看