笔试时间:2023年8月23日 秋招  第一题  题目:开幕式排练  导演在组织进行大运会开幕式的排练,其中一个环节是需要参演人员围成一个环形。演出人员站成了一圈,出于美观度的考虑,导演不希望某一个演员身边的其他人比他低太多或者高太多。  现在给出n个参演人员的身高,问在他们站成一圈时,相邻演员的身高差的最大值至少是多少? 请你帮忙计算。  输入描述  输入包括两行,第一行有1个正整数,代表人数n。  第二行有n个空格隔开的正整数h表示第i个演员的身高。  数据保证2<=n<=10^5,1<=hi<=10^9。  输出描述  输出包括一个正整数,表示答案。  样例输入     5   2 1 1 3 2    样例输出     1    参考题解  将数组排序,然后声明一个新数组,均匀的向两边插入元素,最后遍历一遍数组求出最大值。  C++:  #include <bits/stdc++.h>using namespace std;typedef long long ll;int main(){   int n;   cin>>n;   vector<int> p(n);   for(int i=0;i<n;i++) cin>>p[i];   sort(p.begin(),p.end());   vector<int> ans;   ans.push_back(p[n-1]);   int j=0;   for(int i=n-2;i>=0;i--){      if(j % 2==0) ans.push_back(p[i]);      else ans.insert(ans.begin(),p[i]);      j^=1;   }   int maxx=0;   for(int i=0;i<n;i++)       maxx=max(maxx,abs(ans[i]-ans[(i+1)%n]));   cout <<maxx<< endl;}  Java:  import java.util.*;public class Main {    public static void main(String[] args) {        Scanner sc = new Scanner(System.in);        int n = sc.nextInt();        List<Integer> p = new ArrayList<>();        for (int i = 0; i < n; i++) {            p.add(sc.nextInt());        }        Collections.sort(p);        List<Integer> ans = new ArrayList<>();        ans.add(p.get(n - 1));        int j = 0;        for (int i = n - 2; i >= 0; i--) {            if (j % 2 == 0) {                ans.add(p.get(i));            } else {                ans.add(0, p.get(i));            }            j ^= 1;        }        int maxx = 0;        for (int i = 0; i < n; i++) {            maxx = Math.max(maxx, Math.abs(ans.get(i) - ans.get((i + 1) % n)));        }        System.out.println(maxx);    }}  Python:  n = int(input())
点赞 1
评论 0
全部评论

相关推荐

迷茫的大四🐶:价格这么低都能满了?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务