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.所以以后想问题时,要全面,将题目吃透!
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.所以以后想问题时,要全面,将题目吃透!
相关文章推荐
- A - New Bus Route
- new bus route
- New Bus Route (Codeforces-792A)
- A - New Bus Route
- Educational Codeforces Round 18 A. New Bus Route(水题)
- New Bus Route(第一次打codeforces)
- CodeForces 792A之New Bus Route
- 【CodeForces 792A】New Bus Route
- codeforces 792A - New Bus Route
- UVA 1349 - Optimal Bus Route Design(KM完美匹配)
- uva1349 Optimal Bus Route Design 费用流,二分图匹配
- public static Map<String,List<BusFlight>> device_busFlight = new HashMap<String,List<BusFlight>>();
- UVA 1349 Optimal Bus Route Design
- LA 3353 Optimal Bus Route Design 二分匹配和有向图中的环
- LA 3353 Optimal Bus Route Design
- UVALive 3353 Optimal Bus Route Design
- uva-1349 Optimal Bus Route Design(最小费用最大流)
- UVALive 3353 Optimal Bus Route Design 求图中不相交的哈密顿路的最小和
- UVA-1349 - Optimal Bus Route Design(KM最佳完美匹配)
- UVa 1349 Optimal Bus Route Design