CF - 792A. New Bus Route - 排序

2.中文题意概述:

在一条路上有 n 座城市。每座城市与你现在所处的位置的距离分别为 a1, a2, ..., an。保证a数组的值互不相同。

现在小L想知道,距离最近的两座城市之间的距离是多少,又有多少对城市之间的距离最近。

Input

第一行包含一个大整数 n (2 ≤ n ≤ 2·105).

第二行包含 n 个整数 a1, a2, ..., an ( - 109 ≤ ai ≤ 109). 保证所有 ai 互不相同。

Output

输出两个整数 — 最近的距离和距离最近的城市对数。

Example
Input
4
6 -3 0 4
Output
2 1
Input
3
-2 0 2
Output
2 2
Note

第一个样例中最近的两个城市是第一座城市和第四座城市,距离为 |4 - 6| = 2。只有这两座城市之间的距离是2。

3.解题思路:

排序以后求相邻城市距离,用map维护一下最小值的个数。

4.AC代码:

#include <bits/stdc++.h>
#define INF 0x7fffffff
#define maxn 200100
#define N 1111
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
using namespace std;
const int mod = 1e9 + 7;
typedef long long ll;
typedef unsigned long long ull;
int a[maxn];
map<int, int> mp;
int main()
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	long _begin_time = clock();
#endif
	int n;
	while (~scanf("%d", &n))
	{
		mp.clear();
		for (int i = 0; i < n; i++)
			scanf("%d", &a[i]);
		sort(a, a + n);
		int ans_min = INF;
		for (int i = 1; i < n; i++)
		{
			int res = a[i] - a[i - 1];
			mp[res]++;
			ans_min = min(ans_min, res);
		}
		printf("%d %d\n", ans_min, mp[ans_min]);
	}

#ifndef ONLINE_JUDGE
	long _end_time = clock();
	printf("time = %ld ms.", _end_time - _begin_time);
#endif
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值