SCAU2020春季个人排位赛div2 #2 A

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.
Input
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).

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

Examples
Input
2
1 2
Output
1 1
Input
3
1 4 5
Output
4 1
Input
5
3 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:
分析:这一道题就是问你n个数的最大差于与简单的排列组合问题。
首先将数用数组存起来,然后用sort排个序,b[n-1]-b[0]即为最大差值,然后就是来分析排列组合问题。有一种情况,就是数都相同,于是便Cn2——n个数中选2个。如果不同,则为最大值个数*最小值个数。
好的,我的方法麻烦了,实际上就是取最大最小值,然后分别看有多少个。


```cpp
在这里
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll b[200010];
int main()
{
	memset(b,0,sizeof(b));
	ll i,j,k,n,sum=0,x;
	scanf("%lld",&n);
	for(i=0;i<n;i++){
		scanf("%lld",&b[i]);
	}
	sort(b,b+n);
	if(b[0]==b[n-1])sum=(n-1)*n/2;
	else {
		for(j=1;j<n;j++)if(b[j]!=b[j-1])break;
		for(k=n-2;k>=0;k--)if(b[k]!=b[k+1])break;
		sum=(j)*(n-1-k);
		//printf("%lld====",j);
	}
	x=b[n-1]-b[0];
	printf("%lld %lld",x,sum);
}插入代码片

发布了6 篇原创文章 · 获赞 8 · 访问量 146

猜你喜欢

转载自blog.csdn.net/weixin_45981189/article/details/104788104