New Bus Rote codeforces
来源:互联网 发布:编程开发在哪里学 编辑:程序博客网 时间:2024/06/05 17:32
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.
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.
Print two integer numbers — the minimal distance and the quantity of pairs with this distance.
46 -3 0 4
2 1
3-2 0 2
2 2
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;}
- New Bus Rote codeforces
- CodeForces 792A之New Bus Route
- 【CodeForces 792A】New Bus Route
- CodeForces 792A New Bus Route
- New Bus Route (Codeforces-792A)
- Codeforces Bayan Bus
- 【CODEFORCES】 A. Bayan Bus
- Codeforces Bus to Udayland
- 【Codeforces 864 C. Bus】
- CodeForces 864C Bus
- Codeforces 864 C Bus
- Queue on Bus Stop CodeForces
- codeforces#71 A Bus Game----DFS
- Codeforces 435A Queue on Bus Stop
- codeforces-#475A. Bayan Bus(模拟)
- codeforces 660B. Seating On Bus
- Codeforces 660B Seating On Bus 【模拟】
- CodeForces 660 B. Seating On Bus【模拟】
- POJ NOI MATH-7657 连乘积末尾0的个数
- 如何禁止Mathtype在公式后面自动添加一个空格
- 防干锅51单片机报警程序——很简单的小尝试
- ACM-麦森数
- HNOI 2008 玩具装箱Toy 斜率优化DP
- New Bus Rote codeforces
- 数据结构实验之链表五:单链表的拆分
- vb.net 教程 5-13 图像处理之像素处理 3
- Java非递归实现多叉树型结构遍历成JSON字符串
- webmagic模拟登录方案
- 第五六单元练习
- TensorFlow入门( 一)--基本用法
- 一项被忽视的注入技术(过滤不严,长字符串截断注入)
- Python 简介