CodeForces 792A New Bus Route之超时处理

题目来自:
https://blog.csdn.net/eseszb/article/details/69525736
题目如下:
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.

Examples
Input

4
6 -3 0 4

Output

2 1

Input

3
-2 0 2

Output

2 2

Note

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.
#include<cstdio>
#include<math.h>
using namespace std;
int main()
{
    int n;
    int k = 0;
    int a[200000];
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
    int temp = abs(a[0] - a[1]);
    for (int i = 0; i < n; i++)
    {
        for (int j = i+1; j < n; j++)
        {
            if (temp >= abs(a[i] - a[j]))
            {
                if (temp == abs(a[i] - a[j]))
                {
                    k++;
                }
                else
                {
                    temp = abs(a[i] - a[j]);
                    k = 1;
                }
            }
        }
    }
    printf("%d %d", temp, k);
    return 0;
}

总结:一开始的做法是穷举(把每种情况的距离都算),但是T了,应该要先sort
改正后(不一定最简)

#include<cstdio>
#include<math.h>
#include<algorithm>
using namespace std;
long long a[200005];
int main()
{
    long long n;
    while (~scanf_s("%lld", &n))
    {
        for (long long i = 0; i < n; i++)
        {
            scanf_s("%lld", &a[i]);
        }
        sort(a, a+n);
        long long temp = a[1] - a[0];
        long long k = 1;
        for (long long i = 1; i < n - 1; i++)
        {
            if (a[i + 1] - a[i] == temp)
            {
                 k++;
            }
            else if (a[i + 1] - a[i] < temp)
            {
                temp = a[i + 1] - a[i];
                k = 1;
            }
        }
        printf("%lld %lld\n", temp, k);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值