今天先看了一下二叉树的基本概念和代码,对着代码边敲遍理解,然后删除,关掉代码,自己再敲,反反复复。
主要的重心还是在今天的测试,周总结。
先说周总结:bfs还是要加强,不能太过依赖dfs,然后是dp比较薄弱。
其次就是测试,因为蒟蒻只写出了一道题,还是投机取巧。说明能力确实还不够,还得继续努力。
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 a_iai and Hazem will choose a number a_jaj.
Count the number of interesting pairs (a_i, a_j)(ai,aj) that meet all the following conditions:
- 1 \le i, j \le n1≤i,j≤n;
- i \neq ji=j;
- The absolute difference |a_i - a_j|∣ai−aj∣ must be equal to the maximum absolute difference over all the pairs in the array. More formally, |a_i - a_j| = \max_{1 \le p, q \le n} |a_p - a_q|∣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 \le t \le 1001≤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 \le n \le 10^52≤n≤105).
The second line of each test case contains nn integers a_1, a_2, \dots, a_na1,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 (a_i, a_j)(ai,aj).
Sample 1
Inputcopy | Outputcopy |
---|---|
2 5 6 2 3 8 1 6 7 2 8 3 2 10 | 2 4 |
Note
In the first example, the two ways are:
- Hossam chooses the fourth number 8 and Hazem chooses the fifth number 1.
- Hossam chooses the fifth number 1 and Hazem chooses the fourth number 8.
In the second example, the four ways are:
- Hossam chooses the second number 2 and Hazem chooses the sixth number 10
- Hossam chooses the sixth number 10 and Hazem chooses the second number 2.
- Hossam chooses the fifth number 2 and Hazem chooses the sixth number 10.
- Hossam chooses the sixth number 10 and Hazem chooses the fifth number 2.
今天测试的题目,做题的时候首先翻译了一下,发现求绝对值之差最大的两个数,也就是找最大值和最小值。
-
兴冲冲的看了下样例输出一,发现最大最小值还调换了位置,回去看了下题目,题目只要求绝对值之差最大的序列,没有规定两个数的位置,说明要乘以二。
-
又看了第二个例子,给了两个最小值和一个最大值,然后输出答案就变成了4。所以就猜想了下是不是要乘以个数,然后再回去看题,发现题目的要求也就只有一个,找求绝对值之差最大的序列,就确定了是要乘以最大值最小值的个数的。
-
想到既要求最大值最小值又要求个数于是就用了桶排。然后就交了代码上去,发现没过。
-
然后回去看了下题目的数据,10^5次方,要用long long ,又交了代码上去,发现还是没过。
-
到这里才意识到可能我漏了什么。然后不断检查代码,发现代码完全没问题,然后自己又改了数据,自己算了遍答案发现和程序运行的一样,还是没问题。就觉得题目是不是有问题。
-
然后就是不断的用程序改数据,直到最后试了5个相同的数,发现和自己算的答案不对。才发现这道题的坑,题目只要求绝对值相差最小的两个数,最大值和最小值可能是相同的,这才急忙去改代码。然后就过了。
-
但是说实话,
做的时候有点投机取巧,因为题目特别好心的给了两个例子,非常感谢样例说明。#include <stdio.h> long long int a[100005]; int t,n; int main() { scanf("%d",&t); while(t--) { scanf("%d",&n); long long int cnt=0; long long int k; long long int min=99999; long long int max=-111111; for(int i=0; i<n; i++) { scanf("%lld",&k); a[k]++; if(k<=min) min=k; if(k>=max) max=k; } if(max!=min) { cnt=(a[min])*(a[max])*2; } else { cnt=a[min]*(a[min]-1); } printf("%lld\n",cnt); for(int i=0; i<100005; i++) a[i]=0; } return 0; }