CodeForces 792A之New Bus Route
2017-04-07 12:41
288 查看
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
Output
Input
Output
2 2
题意:给你n个数,问你任意两个之差的绝对值最小为多少,这样的组合有多少个?
AC代码如下:
#include<iostream>
#include<algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=2*1e5+100;
typedef long long llint;
llint n;
llint mai[maxn],num;
llint a[maxn];
int main()
{
while(cin>>n)
{
memset(mai,0,sizeof(mai));
memset(a,0,sizeof(a));
num=0;
for(int i=0;i<n;i++) cin>>a[i];
sort(a,a+n);
llint maxi=9999999999;
llint count=0;
for(int i=0;i<n-1;i++)
{
mai[num]=a[i+1]-a[i];
if(mai[num]<maxi) maxi=mai[num];
num++;
}
for(int i=0;i<num;i++)
{
if(mai[i]==maxi)
count++;
}
cout<<maxi<<" "<<count<<endl;
}
return 0;
}
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个数,问你任意两个之差的绝对值最小为多少,这样的组合有多少个?
AC代码如下:
#include<iostream>
#include<algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=2*1e5+100;
typedef long long llint;
llint n;
llint mai[maxn],num;
llint a[maxn];
int main()
{
while(cin>>n)
{
memset(mai,0,sizeof(mai));
memset(a,0,sizeof(a));
num=0;
for(int i=0;i<n;i++) cin>>a[i];
sort(a,a+n);
llint maxi=9999999999;
llint count=0;
for(int i=0;i<n-1;i++)
{
mai[num]=a[i+1]-a[i];
if(mai[num]<maxi) maxi=mai[num];
num++;
}
for(int i=0;i<num;i++)
{
if(mai[i]==maxi)
count++;
}
cout<<maxi<<" "<<count<<endl;
}
return 0;
}
相关文章推荐
- 【CodeForces 792A】New Bus Route
- codeforces 792A - New Bus Route
- New Bus Route (Codeforces-792A)
- New Bus Route(第一次打codeforces)
- A - New Bus Route
- Educational Codeforces Round 18 A. New Bus Route(水题)
- new bus route
- A - New Bus Route
- A - New Bus Route
- Codeforces 500C New Year Book Reading
- codeforces Good Bye 2016-D New Year and Fireworks(dfs)
- UVA 1349 Optimal Bus Route Design——最小权完美匹配
- 【CodeForces】500B - New Year Permutation(Floyd)(贪心)
- Codeforces_611D:New_Year_and_Ancient_Prophecy(DP+树状数组)
- UVA 1349 Optimal Bus Route Design(最小权完美匹配)
- 【CodeForces】500C - New Year Book Reading(贪心)
- Problem--379A--Codeforces--A. New Year Candles
- Codeforces Good Bye 2017 F - New Year and Rainbow Roads
- CodeForces 611 D. New Year and Ancient Prophecy(dp)
- Codeforces Good Bye 2015 A. New Year and Days