New Bus Route(第一次打codeforces)
2017-03-28 15:33
363 查看
LOL。。
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.
题目就不说了 ,说一下自己怎么错的吧
代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int a[2000010];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n);
long long min=100000000000;
for(int i=n-1; i>=1; i--)
{
if(a[i]-a[i-1]<min)
{
min=a[i]-a[i-1];
}
}
int ans=0;
for(int i=n-1; i>=1; i--)
{
if(a[i]-a[i-1]==min)
{
ans++;
}
}
printf("%d %d\n",min,ans);
}
}
开始一看,蛮简单的,写完了结果老是test4出错。。。
后来发现最大数弄小了,把min换成min=100000000000一下就AC了
还是经验不够
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.
题目就不说了 ,说一下自己怎么错的吧
代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int a[2000010];
int main()
{
int n;
while(~scanf("%d",&n))
{
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
sort(a,a+n);
long long min=100000000000;
for(int i=n-1; i>=1; i--)
{
if(a[i]-a[i-1]<min)
{
min=a[i]-a[i-1];
}
}
int ans=0;
for(int i=n-1; i>=1; i--)
{
if(a[i]-a[i-1]==min)
{
ans++;
}
}
printf("%d %d\n",min,ans);
}
}
开始一看,蛮简单的,写完了结果老是test4出错。。。
后来发现最大数弄小了,把min换成min=100000000000一下就AC了
还是经验不够
相关文章推荐
- New Bus Route (Codeforces-792A)
- codeforces 792A - New Bus Route
- 【CodeForces 792A】New Bus Route
- CodeForces 792A之New Bus Route
- A - New Bus Route
- A - New Bus Route
- A - New Bus Route
- Educational Codeforces Round 18 A. New Bus Route(水题)
- new bus route
- 1349 - Optimal Bus Route Design(二分图最小权完美匹配)
- CodeForces 611 D. New Year and Ancient Prophecy(dp)
- codeforces 723A : The New Year: Meeting Friends
- uva1349 Optimal Bus Route Design 费用流,二分图匹配
- 【CodeForces】500B - New Year Permutation(Floyd)(贪心)
- Codeforces Good Bye 2017 C. New Year and Curling(模拟水题)
- New Year Snowmen CodeForces - 140C
- codeforces-611C-New Year and Domino
- Codeforces_New Year and Arbitrary Arrangement
- New Bus Rote codeforces
- Codeforces Good Bye 2015 D. New Year and Ancient Prophecy 后缀数组 树状数组 dp