求差的绝对值最小的值及其个数

欢迎访问https://blog.csdn.net/lxt_Lucia~~

宇宙第一小仙女\(^o^)/~~萌量爆表求带飞=≡Σ((( つ^o^)つ~dalao们点个关注呗~~
 

------------------------------------我只是一条可爱哒分界线--------------------------------------

 

话不多说,Problem is coming again~~~~

 

Description

There are n cities situated along the main road of Berland. Cities are represented by their coordinates — integer numbers a1, a2, ..., an. All coordinates are pairwise distinct.

It is possible to get from one city to another only by bus. But all buses and roads are very old, so the Minister of Transport decided to build a new bus route. The Minister doesn't want to spend large amounts of money — he wants to choose two cities in such a way that the distance between them is minimal possible. The distance between two cities is equal to the absolute value of the difference between their coordinates.

It is possible that there are multiple pairs of cities with minimal possible distance, so the Minister wants to know the quantity of such pairs.

Your task is to write a program that will calculate the minimal possible distance between two pairs of cities and the quantity of pairs which have this distance.

 

Input

The first line contains one integer number n (2 ≤ n ≤ 2·105).

The second line contains n integer numbers a1, a2, ..., an ( - 109 ≤ ai ≤ 109). All numbers ai are pairwise distinct.

 

Output

Print two integer numbers — the minimal distance and the quantity of pairs with this distance.

 

Input

4
6 -3 0 4

Output

2 1

Input

3
-2 0 2

Output

2 2

 

Hint

In the first example the distance between the first city and the fourth city is |4 - 6| = 2, and it is the only pair with this distance.

 

题意:求这n个数,任意两个数的差的绝对值中的最小值为多少,并求其出现次数

思路:

1.还有一种最常规的思路就是套双重循环遍历,一个个减,然后找最小值,虽然也不错,但是数据量要是很大的话是会时间超限的,还好我聪明,选择了第二种方法,wow怎么这么机智哈哈哈~

2.因为题目不涉及最开始的序号,所以可以直接快排。然后每个都用后一个减前一个找差值的最小值,毕竟一组数据里,和自己差值最小的数一定是和自己最近的数嘛.....至于个数....如果和原先的最小值相等,就k++咯,如果最小绝对差值比原先的小,k要更新为1咯,其他的话,就不用管咯~下面的代码就是这个思路。

 

继续hint一下.........数组最好开long long int ,k注意清1,fabs()是求绝对值的函数,类似的函数还有平方根函数sqrt(),幂函数pow(a,b)代表a的b此方。。。话不多说,上代码。。。。。

 

--------------------------------------------------------------------我只是一条可爱的分界线----------------------------------------------------------------------------

 

 

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,k=1,min;
    long long int a[1000000],z;
    scanf("%d",&n);
    for(i=0;i<=n-1;i++)
       scanf("%lld",&a[i]);
    sort(a,a+n);
    min=fabs(a[1]-a[0]);
    for(i=2;i<=n-1;i++){
        z=fabs(a[i]-a[i-1]);
        if(z==min)
            k++;
        else if(z<min){
            min=z;
            k=1;
        }
    }
    printf("%d %d\n",min,k);
    return 0;
}<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,k=1,min;
    long long int a[1000000],z;
    scanf("%d",&n);
    for(i=0;i<=n-1;i++)
       scanf("%lld",&a[i]);
    sort(a,a+n);
    min=fabs(a[1]-a[0]);
    for(i=2;i<=n-1;i++){
        z=fabs(a[i]-a[i-1]);
        if(z==min)
            k++;
        else if(z<min){
            min=z;
            k=1;
        }
    }
    printf("%d %d\n",min,k);
    return 0;
}

 

-----------------------------------------我也是有底线的-----------------------------------------

 

宇宙第一小仙女\(^o^)/~~萌量爆表求带飞=≡Σ((( つ^o^)つ~dalao们点个关注呗~~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值