Educational Codeforces Round 18 A
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.
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.
4
6 -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.
题意:问两点之间距离最小为多少,有几对距离最小的
解法:排序模拟
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define ll long long 4 ll n; 5 ll a[300000]; 6 ll maxn=(1<<31)-1; 7 int main() 8 { 9 cin>>n; 10 for(int i=1;i<=n;i++) 11 { 12 cin>>a[i]; 13 } 14 sort(a+1,a+1+n); 15 ll pos=a[1]; 16 for(int i=2;i<=n;i++) 17 { 18 maxn=min(abs(a[i]-pos),maxn); 19 pos=a[i]; 20 } 21 cout<<maxn<<" "; 22 ll ans=a[1]; 23 ll sum=0; 24 for(int i=2;i<=n;i++) 25 { 26 if(maxn==abs(a[i]-ans)) 27 { 28 sum++; 29 } 30 ans=a[i]; 31 } 32 cout<<sum<<endl; 33 return 0; 34 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~