B. Pashmak and Flowers

来源:互联网 发布:淘宝代运营 猪八戒网 编辑:程序博客网 时间:2024/05/21 09:54

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Pashmak decided to give Parmida a pair of flowers from the garden. There are n flowers in the garden and the i-th of them has a beauty number bi. Parmida is a very strange girl so she doesn't want to have the two most beautiful flowers necessarily. She wants to have those pairs of flowers that their beauty difference is maximal possible!

Your task is to write a program which calculates two things:

  1. The maximum beauty difference of flowers that Pashmak can give to Parmida.
  2. The number of ways that Pashmak can pick the flowers. Two ways are considered different if and only if there is at least one flower that is chosen in the first way and not chosen in the second way.
Input

The first line of the input contains n (2 ≤ n ≤ 2·105). In the next line there are n space-separated integers b1b2, ..., bn (1 ≤ bi ≤ 109).

Output

The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively.

Sample test(s)
input
21 2
output
1 1
input
31 4 5
output
4 1
input
53 1 2 3 1
output
2 4
Note

In the third sample the maximum beauty difference is 2 and there are 4 ways to do this:

  1. choosing the first and the second flowers;
  2. choosing the first and the fifth flowers;
  3. choosing the fourth and the second flowers;
  4. choosing the fourth and the fifth flowers.

解题说明:此题是找出一列数中差值最大的一对数,很显然是找出该数列中的最大数和最小数。由于可能存在多个最大数和最小数,在构造不同的配对时,只需要保证每个配对中有一个数字不同即可,是个简单的排列组合问题。要注意的是,如果该数列中所有数都相同,那么就是C(n,2)


#include<iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<cstdlib>#include<cstring>using namespace std;int main(){long long int n;long long int b[200000];long long int i=0,j=0,k=0,n1=1,n2=1;scanf("%lld",&n);for(i=0;i<n;i++){scanf("%lld",&b[i]);}j=b[0];k=b[0];for(i=1;i<n;i++){if(b[i]>j){j=b[i];n1=1;}else if(b[i]==j){n1++;}if(b[i]<k){k=b[i];n2=1;}else if(b[i]==k){n2++;}}if(j!=k){printf("%lld %lld\n",j-k,n1*n2);}else{printf("%lld %lld\n",j-k,(n1*(n1-1))/2);}return 0;}


0 0