给定一个长度为n的序列a,要求计算一共有多少对(i,j)使得abs(a[i]-a[j])=序列a中最大的abs(a[x]-a[y]),其中i!=j,x!=y;
Hossam woke up bored, so he decided to create an interesting array with his friend Hazem.
Now, they have an array aa of nn positive integers, Hossam will choose a number ai and Hazem will choose a number aj.
Count the number of interesting pairs (ai,aj) that meet all the following conditions:
- 1≤i,j≤n;
- i!=j;
- The absolute difference ∣ai−aj∣ must be equal to the maximum absolute difference over all the pairs in the array. More formally, ∣ai−aj∣=max1≤p,q≤n∣ap−aq∣.
Input
The input consists of multiple test cases. The first line contains a single integer tt ( 1≤t≤100), which denotes the number of test cases. Description of the test cases follows.
The first line of each test case contains an integer nn (2≤n≤105).
The second line of each test case contains nn integers a1,a2,…,an (1 \le a_i \le 10^51≤ai≤105).
It is guaranteed that the sum of nn over all test cases does not exceed 10^5105.
Output
For each test case print an integer — the number of interesting pairs(ai,aj).
Sample 1
Input | Output |
---|---|
2 5 6 2 3 8 1 6 7 2 8 3 2 10 | 2 4 |
本题我们可以先求出最大最小值,然后求出最大最小值的数量下x,y。1.如果最大值等于最小值即输入的一组数中所有数相等,则输出等于(x-1)*y。2.如果最大值最小值不等,则输出等于x*y*2.(本题一定要注意数据范围)
int main()
{
int t,n,max,min;
long long j,k,x,y;
scanf("%d",&t);
while(t)
{
k=0;
j=0;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
max=a[0];
min=a[0];
for(int i=0;i<n;i++)
{
if(max<a[i])
max=a[i];
if(min>a[i])
min=a[i];
}
for(int i=0;i<n;i++)
{
if(a[i]==max)
k++;
if(a[i]==min)
j++;
}
x=(k-1)*j;
y=k*j*2;
if(max==min)
printf("%lld\n",x);
else
printf("%lld\n",y);
t--;
}
return 0;
}