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;
    	
    }
    
  • 相关阅读:
    比较对象的相对性
    深拷贝与浅拷贝(TBD)
    创建UI的线程才能访问UI,那么怎样才算访问UI呢
    多层级的数据绑定效果
    众所周知,static修饰的成员只实例化一次,而string类型每次赋值都会重新创建一个实例,那么用static修饰string呢?
    常量、只读字段
    使用dos打开相关软件
    查看电脑硬件信息dos命令
    Windows常用快捷键
    使用外部编辑器出现乱码
  • 原文地址:https://www.cnblogs.com/-yjun/p/10424257.html
Copyright © 2011-2022 走看看