• 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;
    	
    }
    
  • 相关阅读:
    Web标准:五、超链接伪类
    Spring Security(16)——基于表达式的权限控制
    Spring Security(15)——权限鉴定结构
    Spring Security(14)——权限鉴定基础
    Spring Security(13)——session管理
    Spring Security(12)——Remember-Me功能
    Spring Security(11)——匿名认证
    Spring Security(10)——退出登录logout
    Spring Security(09)——Filter
    Spring Security(08)——intercept-url配置
  • 原文地址:https://www.cnblogs.com/-yjun/p/10424257.html
Copyright © 2020-2023  润新知