欢迎光临散文网 会员登陆 & 注册

CF 1771A - Hossam and Combinatorics

2023-07-05 08:15 作者:您是打尖儿还是住店呢  | 我要投稿

Hossam woke up bored, so he decided to create an interesting array with his friend Hazem.

Now, they have an array a of n 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 t (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 n (2≤n≤105).


The second line of each test case contains n integers a1,a2,…,an (1≤ai≤105).

It is guaranteed that the sum of n over all test cases does not exceed 105.

Output

For each test case print an integer — the number of interesting pairs (ai,aj).

---------------------------------------------------------------

Hossam 醒来感到很无聊,所以他决定和他的朋友 Hazem 一起创建一个有趣的数组。

现在,他们有一个由 n 个正整数组成的数组 a,Hossam 将选择一个数字 ai,Hazem 将选择一个数字 aj。

计算满足以下所有条件的有趣对 (ai,aj) 的数量:1≤i,j≤n;i≠j;

绝对差|ai−aj| 必须等于数组中所有对的最大绝对差。 更正式地说,|ai−aj|=max1≤p,q≤n|ap−aq|。

输入

输入由多个测试用例组成。 第一行包含一个整数t(1≤t≤100),表示测试用例的数量。 测试用例的描述如下。

每个测试用例的第一行包含一个整数n(2≤n≤105)。


每个测试用例的第二行包含n个整数a1,a2,…,an

  (1≤ai≤105)。

保证所有测试用例的n之和不超过105。

输出

对于每个测试用例,打印一个整数 - 有趣的对 (ai,aj) 的数量。

----------------------------

踩了2个坑,一个是乘积会溢出的情况,没有看示例的数据范围,一个是没有考虑极端情况,也就是所有的值全部相当的时候。

其他没什么问题的,下面是代码:


CF 1771A - Hossam and Combinatorics的评论 (共 条)

分享到微博请遵守国家法律