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

本文介绍了一个有趣的算法问题:从一组给定的整数中找出具有最小和最大绝对差值的数对,并计算这样的数对数量。文章提供了详细的解决思路与实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值