您的位置:首页 > 其它

A - New Bus Route

2017-03-29 23:23 162 查看
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.

Sample Input

Input

4

6 -3 0 4

Output

2 1

Input

3

-2 0 2

Output
2 2

题目描述:

题目情形理解不是很清楚,但大概就是给你的n组数据中,让你找到两两之差中最小的,然后输出,并且还要输出它出现的次数!

解题思路:

运用数组,然后进行排序,后面的减去前面的,然后一定会有最小的一个值!

解题细节:

注意一些解题技巧,比如if(t<s) s=t,c=1;即找到一个最小的数,将它的个数置为1,如果最小的和它相同则继续相加!

代码:

#include<cstdio>
#include<cmath>
#include<iostream>
#include<algorithm>
#define N 210000
using namespace std;
int n,a
,s,c;
int main()
{
cin>>n;
for(int i=1;i<=n;i++)
cin>>a[i];
s=(2e9)+1;
sort(a+1,a+n+1);
for(int i=1;i<n;i++)
{
int t=a[i+1]-a[i];
if(t<s) s=t,c=1;
else if(t==s) c++;
}
cout<<s<<' '<<c<<endl;
return 0;
}心得:
感觉自己好水,一开始将问题想的复杂化,导致迟迟不能AC.所以以后想问题时,要全面,将题目吃透!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: