文章标题 SPOJ INUM : Interesting Numbers (细节)

来源:互联网 发布:丢火车知乎 编辑:程序博客网 时间:2024/05/21 17:47

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.
题意:给你n个数a[n],然后任意找出其中的两个数a[i],a[j],使得其绝对值|a[i]-a[j]|最小和最大,求出绝对值最小和最大的对数。
分析:首先,将n个数排序,然后去重,把重复的数字的次数c[i]记下来,然后有以下几种情况
1、n=1,则maxn和minx都为0
2、n=2,则maxn和minx都为1
3、n>=3时,maxn的值都是c[0]*c[cnt] (cnt为这n个数中不同数字的数目)
而当cnt=n时,说明得通过相邻的两个数相减获得
当cnt

#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;}
0 0
原创粉丝点击