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:
- The maximum beauty difference of flowers that Pashmak can give to Parmida.
- 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.
The first line of the input contains n (2 ≤ n ≤ 2·105). In the next line there are n space-separated integers b1, b2, ..., bn (1 ≤ bi ≤ 109).
The only line of output should contain two integers. The maximum beauty difference and the number of ways this may happen, respectively.
2 1 2
1 1
3 1 4 5
4 1
5 3 1 2 3 1
2 4
In the third sample the maximum beauty difference is 2 and there are 4 ways to do this:
- choosing the first and the second flowers;
- choosing the first and the fifth flowers;
- choosing the fourth and the second flowers;
- choosing the fourth and the fifth flowers.
这好像比A还水……输出极差,和两个数的差等于极差的方案数
然后直接输出最大值的个数乘最小值的个数好像会wa……我会告诉你n个1的情况答案是n*(n-1)/2吗
#include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<cmath> #include<algorithm> using namespace std; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int n,mx,mn; int a[200010]; long long ans,m1,m2; int main() { n=read(); for (int i=1;i<=n;i++)a[i]=read(); sort(a+1,a+n+1); mn=a[1];mx=a[n]; for (int i=1;i<=n;i++) { if (a[i]==mx)m1++; if (a[i]==mn)m2++; } ans=m1*m2;if (mx==mn)ans=(long long)(n-1)*n/2; printf("%d %lld",mx-mn,ans); }