New Bus Rote codeforces
2017-04-04 22:54
92 查看
A. 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
output
input
output
Note
In the first example the distance between the first city and the fourth city is |4 - 6| = 2, and it is the only pair with this distance.
题目大意:给出若干个数,求任意两个数差的绝对值最小的值。
解题思路:从小到大排序,然后相邻数做差,然后维护这个差的最小值。
当时最大值INF定义的0x3f3f3f3f,因为这个导致这题错误。其实用INT_MAX即可。
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int INF = INT_MAX;
const int maxn = 2 * 1e5 + 10000;
int a[maxn];
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; ++i) {
cin >> a[i];
}
sort(a, a + n);
int x = INF;
for(int i = 1; i < n; ++i) {
x = min (x, abs(a[i] - a[i - 1]));
}
int y = 0;
for(int i = 1; i < n; ++i) {
if(abs(a[i] - a[i - 1]) == x) {
y++;
}
}
cout << x << " " << y << endl;
return 0;
}
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
Note
In the first example the distance between the first city and the fourth city is |4 - 6| = 2, and it is the only pair with this distance.
题目大意:给出若干个数,求任意两个数差的绝对值最小的值。
解题思路:从小到大排序,然后相邻数做差,然后维护这个差的最小值。
当时最大值INF定义的0x3f3f3f3f,因为这个导致这题错误。其实用INT_MAX即可。
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
const int INF = INT_MAX;
const int maxn = 2 * 1e5 + 10000;
int a[maxn];
int main()
{
int n;
cin >> n;
for(int i = 0; i < n; ++i) {
cin >> a[i];
}
sort(a, a + n);
int x = INF;
for(int i = 1; i < n; ++i) {
x = min (x, abs(a[i] - a[i - 1]));
}
int y = 0;
for(int i = 1; i < n; ++i) {
if(abs(a[i] - a[i - 1]) == x) {
y++;
}
}
cout << x << " " << y << endl;
return 0;
}
相关文章推荐
- Educational Codeforces Round 18 A. New Bus Route(水题)
- A - New Year Table CodeForces - 140A(精度,几何图形)
- New Year Tree CodeForces - 620E
- Codeforces Round #346 (Div. 2) E. New Reform
- Educational Codeforces Round 6 E. New Year Tree(DFS序+线段树)
- New Year and Curling CodeForces - 908C
- Codeforces Round #436 (Div. 2) C. Bus
- New Year and Old Property CodeForces - 611B
- Codeforces Round #346 (Div. 2)E. New Reform 乱搞dfs
- D - New Year Table CodeForces - 140A——double精度+弧度角度转化
- Codeforces Round #436 (Div. 2) C. Bus
- Codeforces Round #150 (Div. 2) C. The Brand New Function
- Codeforces Round #267 (Div. 2) B. Fedor and New Game
- 模拟好题 E - Bus CodeForces - 864C
- C - New Year and Hurry CodeForces - 750A(动态规划)
- Codeforces New Year Candles
- Codeforces Round #375 (Div. 2) A. The New Year: Meeting Friends 水题
- codeforces GoodBye2015 D.New Year and Ancient Prophecy (dp+lcp+树状数组)
- E - New Year and North Pole CodeForces - 750B(状态处理下就好了)
- Codeforces The New Year: Meeting Friends