Honest Coach

举报
辰chen 发表于 2022/06/20 23:43:02 2022/06/20
【摘要】 文章目录 一、B. Honest Coach总结 一、B. Honest Coach 本题链接:B. Honest Coach 题目: B. Honest Coach time ...


一、B. Honest Coach

本题链接B. Honest Coach

题目

B. Honest Coach

time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output

There are n athletes in front of you. Athletes are numbered from 1 to n from left to right. You know the strength of each athlete — the athlete number i has the strength si.

You want to split all athletes into two teams. Each team must have at least one athlete, and each athlete must be exactly in one team.

You want the strongest athlete from the first team to differ as little as possible from the weakest athlete from the second team. Formally, you want to split the athletes into two teams A and B so that the value |max(A)−min(B)| is as small as possible, where max(A) is the maximum strength of an athlete from team A, and min(B) is the minimum strength of an athlete from team B.

For example, if n=5 and the strength of the athletes is s=[3,1,2,6,4], then one of the possible split into teams is:

first team: A=[1,2,4],
second team: B=[3,6].
In this case, the value |max(A)−min(B)| will be equal to |4−3|=1. This example illustrates one of the ways of optimal split into two teams.

Print the minimum value |max(A)−min(B)|.

Input
The first line contains an integer t (1≤t≤1000) — the number of test cases in the input. Then t test cases follow.

Each test case consists of two lines.

The first line contains positive integer n (2≤n≤50) — number of athletes.

The second line contains n positive integers s1,s2,…,sn (1≤si≤1000), where si — is the strength of the i-th athlete. Please note that s values may not be distinct.

Output
For each test case print one integer — the minimum value of |max(A)−min(B)| with the optimal split of all athletes into two teams. Each of the athletes must be a member of exactly one of the two teams.

Example
input
5
5
3 1 2 6 4
6
2 1 3 2 4 3
4
7 9 3 1
2
1 1000
3
100 150 200
output
1
0
2
999
50

Note
The first test case was explained in the statement. In the second test case, one of the optimal splits is A=[2,1], B=[3,2,4,3], so the answer is |2−2|=0.

本博客给出本题截图
在这里插入图片描述
在这里插入图片描述

题意:把一个数组分成两个数组A和B,求|max(A)−min(B)|的最小值

AC代码

#include <iostream>
#include <algorithm>

using namespace std;

const int N = 55;

int a[N];

int main()
{
    int t;
    cin >> t;
    
    while (t -- )
    {
        int n;
        cin >> n;
        for (int i = 0; i < n; i ++ ) 
            cin >> a[i];
            
        sort(a, a + n);
        
        int res = a[1] - a[0];
        for (int i = 1, j = 2; j < n; i ++, j ++ )
            res = min(res, a[j] - a[i]);
            
        cout << res << endl;
    }
    
    return 0;
}

  
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

总结

水题,不解释

文章来源: chen-ac.blog.csdn.net,作者:辰chen,版权归原作者所有,如需转载,请联系作者。

原文链接:chen-ac.blog.csdn.net/article/details/119004502

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。