# 文章标题 SPOJ INUM : Interesting Numbers （细节）

## Interesting Numbers

Jack and Jill went up the hill. Jack proposed Jill after reaching at the top of the hill. Jill gave Jack ‘N’ numbers and asked him to choose a pair of numbers such that their absolute difference is minimum. She also asked him to choose a pair of numbers such that their absolute difference is maximum. Jill wondered just finding maximum and minimum values is mainstream and it will be a cakewalk for Jack, instead she asked him to find number of pairs which have minimum absolute difference and number of pairs which have maximum absolute difference.

Jill will accept Jack’s proposal only if he can answer her question correctly. Jack does not know programming. Fortunately Jack had his laptop with him, fully charged and with good internet connection. He mailed you the numbers and asked you to mail him the answers back, as you are known to be a good programmer. Now it’s your turn to help him.

Input Format
First line contains a positive integer ‘N’.
Next line contains ‘N’ non-negative integers ‘a[i]’ separated by a single space.

Output Format
Print the number of pairs having minimum and maximum difference separated by a single space.

Constraints
1 ≤ N ≤ 105
0 ≤ a[i] ≤ 1015

Sample Input
5
5 12 8 1 35

Sample Output
1 1

Explanation
It’s optimal to choose the pair (5, 8) for the minimum difference.So only 1 pair.
It’s optimal to choose the pair (1,35) for the maximum difference. Here also only 1 pair.

1、n=1,则maxn和minx都为0
2、n=2，则maxn和minx都为1
3、n>=3时，maxn的值都是c[0]*c[cnt] （cnt为这n个数中不同数字的数目）

#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<math.h>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;
const int inf = 0x3f3f3f3f;
long long a[100005];
int n;
long long num[100005];
long long c[100005];
int main ()
{
while (scanf ("%d",&n)!=EOF){
int cnt=0;
memset (c,0,sizeof (c));
long long minx=0;
long long maxn=0;
for (int i=0;i<n;i++){
scanf ("%lld",&a[i]);
}
if (n==1){//n为1 时
printf ("0 0\n");
continue;
}
sort(a,a+n);
num[cnt]=a[0];
c[cnt]++;
long long tmp=a[0];
for (int i=1;i<n;i++){
if (a[i]!=tmp){
cnt++;
num[cnt]=a[i];
tmp=a[i];
}
c[cnt]++;
}
if (cnt==0){//只有一个数字出现时
printf ("%lld %lld\n",c[0]*(c[0]-1)/2,c[0]*(c[0]-1)/2);
continue;
}
maxn=c[0]*c[cnt];//不管如何，都是头尾数字次数之积
if (cnt+1<n){//有重复的数字出现
for (int i=0;i<=cnt;i++){
minx+=(c[i]*(c[i]-1)/2);
}
}
else if (cnt+1==n){//没有重复的数字出现
long long tmp=a[1]-a[0];
minx=1;
for (int i=2;i<n;i++){
if (a[i]-a[i-1]==tmp){
minx++;
}
else if (a[i]-a[i-1]<tmp){
minx=1;
tmp=a[i]-a[i-1];
}
}
}
printf ("%lld %lld\n",minx,maxn);
}
return 0;
}
• 本文已收录于以下专栏：

举报原因： 您举报文章：文章标题 SPOJ INUM : Interesting Numbers （细节） 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)