日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Honest Coach CodeForces - 1360B(简单贪心)

發(fā)布時間:2023/12/4 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Honest Coach CodeForces - 1360B(简单贪心) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目:

把所給的數(shù)組分成a和b兩個子數(shù)組(元素不重復使用),令a數(shù)組的的最大值和b數(shù)組的最小值的差最小,并輸出。

題意:

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.

分析:

也就是找數(shù)列中兩元素的差的最小值;先將sort數(shù)組排序,遍歷找相鄰的元素差

AC代碼:

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int M=1e3+10; const int inf=0x3f3f3f3f; int t,n,ans; int dp[M]; int main() {scanf("%d",&t);while(t--){ans=inf;scanf("%d",&n);for(int i=0;i<n;i++)scanf("%d",&dp[i]);sort(dp,dp+n);for(int i=1;i<n;i++)ans=min(ans,dp[i]-dp[i-1]);printf("%d\n",ans);}return 0; }

總結(jié)

以上是生活随笔為你收集整理的Honest Coach CodeForces - 1360B(简单贪心)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。