New Bus Route (Codeforces-792A)
2017-08-14 11:30
295 查看
题目链接:
http://codeforces.com/problemset/problem/792/AA. New Bus Route
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
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
题目大意:
输入n,然后输入n个数,计算着n个数中任意两个数字之差 的绝对值最小值是多少,并输出有多少种这种组合使得差的绝对值最小。解题思路:
先把n这n个数从小到大排序(这样能保证相邻的数字之差的绝对值最小,避免计算差的绝对值较大的情况,节省了运行时间,注意题目要求就1秒),然后分别计算相邻数字之差,比较出来最小值,然后计算最小值的组合数。代码:
#include<iostream> #include<algorithm> #include<map> #include<cmath> using namespace std; int main() { long long int n,a[200020]; while(cin>>n) { map<int,int>M; //用来保存每一种差值得个数(只需输出差值的绝对值最小的个数即可) long long int mins=2000000005; //让mins足够大,保存最小差值的绝对值 for(int i=0;i<=n-1;i++) cin>>a[i]; sort(a,a+n); for(int i=0;i<=n-2;i++) { if(abs(a[i+1]-a[i])<=mins) //mins保存最小差值的绝对值,注意:if条件里面必须是<= { mins=abs(a[i+1]-a[i]); M[mins]++; //对应最小差值的绝对值 个数+1 } } cout<<mins<<' '<<M[mins]<<endl; } return 0; }
相关文章推荐
- codeforces 792A - New Bus Route
- 【CodeForces 792A】New Bus Route
- CodeForces 792A之New Bus Route
- New Bus Route(第一次打codeforces)
- A - New Bus Route
- A - New Bus Route
- Educational Codeforces Round 18 A. New Bus Route(水题)
- new bus route
- A - New Bus Route
- 【CodeForces】500B - New Year Permutation(Floyd)(贪心)
- CodeForces 611 B. New Year and Old Property(水~)
- CodeForces Good Bye 2016 :C New Year and Rating、D New Year and Fireworks、E New Year and Old Subsequ
- CodeForces 379F 树的直径 New Year Tree
- Optimal Bus Route Design UVA 1349
- uva 3353 Optimal Bus Route Design (费用流/KM)
- 【Codeforces】-611A-New Year and Days(水)
- codeforces 500D - New Year Santa Network (树形DP+组合数学)
- Codeforces 491B. New York Hotel 最远曼哈顿距离
- UVa 1349 (二分图最小权完美匹配) Optimal Bus Route Design
- Codeforces Good Bye 2017: F. New Year and Rainbow Roads(模拟)