CodeForces 792A之New Bus Route
来源:互联网 发布:手机网页编辑软件 编辑:程序博客网 时间:2024/05/16 10:28
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
46 -3 0 4
2 1
3-2 0 2
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
- CodeForces 792A New Bus Route
- New Bus Route (Codeforces-792A)
- CF 792A New Bus Route 排序,模拟
- New Bus Rote codeforces
- 【CODEFORCES】 A. Bayan Bus
- Bus Route
- CodeForces 711A A. Bus to Udayland
- codeforces#71 A Bus Game----DFS
- Codeforces 435A Queue on Bus Stop
- codeforces-#475A. Bayan Bus(模拟)
- Codeforces 435A Queue on Bus Stop
- codeforces 711A.Bus to Udayland 【水题】
- Codeforces 711A Bus to Udayland
- 【模拟】Codeforces 711A Bus to Udayland
- Codeforces 711A- Bus to Udayland
- Codeforces-711A-Bus to Udayland
- mmap映射问题
- JS範囲チェック追加
- 波动数列 蓝桥
- 判断是否子树
- Android设备双屏显示
- CodeForces 792A之New Bus Route
- 解决SpringMVC对js css等静态资源拦截
- JAVA——package语句、classpath环境变量配置
- hdu5606 Tree (并查集)
- Programming Assignment 5: Kd-Trees
- 系列:iOS 开发-基础控件
- GC算法和种类
- zookeeper和dubbo的关系
- Softmax回归