zoukankan      html  css  js  c++  java
  • A. Difference Row

    A. Difference Row
    time limit per test
    2 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You want to arrange n integers a1, a2, ..., an in some order in a row. Let's define the value of an arrangement as the sum of differences between all pairs of adjacent integers.

    More formally, let's denote some arrangement as a sequence of integers x1, x2, ..., xn, where sequence x is a permutation of sequence a. The value of such an arrangement is (x1 - x2) + (x2 - x3) + ... + (xn - 1 - xn).

    Find the largest possible value of an arrangement. Then, output the lexicographically smallest sequence x that corresponds to an arrangement of the largest possible value.

    Input

    The first line of the input contains integer n (2 ≤ n ≤ 100). The second line contains n space-separated integers a1a2...an (|ai| ≤ 1000).

    Output

    Print the required sequence x1, x2, ..., xn. Sequence x should be the lexicographically smallest permutation of a that corresponds to an arrangement of the largest possible value.

    Sample test(s)
    input
    5
    100 -100 50 0 -50
    output
    100 -50 0 50 -100 

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 int a[101];
     4 int comp(const void *a,const void *b)
     5 {
     6 return *(int *)a-*(int *)b;
     7 }
     8 
     9 int main()
    10 {
    11 int n,i;
    12 while(~scanf("%d",&n))
    13 {
    14 for(i = 0;i < n;i ++)
    15 scanf("%d",&a[i]);
    16 qsort(a,n,sizeof(a[0]),comp);
    17 printf("%d ",a[n-1]);
    18 for(i = 1;i < n-1;i ++)
    19 printf("%d ",a[i]);
    20 printf("%d
    ",a[0]);
    21 }
    22 return 0;
    23 }
  • 相关阅读:
    [前端开发]Vue组件化的思想
    [前端开发]数组中哪些方法是响应式的
    冒泡排序和选择排序
    css定位属性的运用
    JS拖拽效果的原理及实现
    Js函数的形参和实参详解
    Js中的For循环详解
    什么是盒模型?
    关于使用JS去除URL中的指定参数问题,js 对url进行某个参数的删除,并返回url
    听力的尝试
  • 原文地址:https://www.cnblogs.com/anhuizhiye/p/3331492.html
Copyright © 2011-2022 走看看