All cities of Lineland are located on the Ox coordinate axis. Thus, each city is associated with its position xi — a coordinate on the Oxaxis. No two cities are located at a single point.
Lineland residents love to send letters to each other. A person may send a letter only if the recipient lives in another city (because if they live in the same city, then it is easier to drop in).
Strange but true, the cost of sending the letter is exactly equal to the distance between the sender's city and the recipient's city.
For each city calculate two values mini and maxi, where mini is the minimum cost of sending a letter from the i-th city to some other city, and maxi is the the maximum cost of sending a letter from the i-th city to some other city
The first line of the input contains integer n (2 ≤ n ≤ 105) — the number of cities in Lineland. The second line contains the sequence ofn distinct integers x1, x2, ..., xn ( - 109 ≤ xi ≤ 109), where xi is the x-coordinate of the i-th city. All the xi's are distinct and follow inascending order.
Print n lines, the i-th line must contain two integers mini, maxi, separated by a space, where mini is the minimum cost of sending a letter from the i-th city, and maxi is the maximum cost of sending a letter from the i-th city.
4 -5 -2 2 7
3 12 3 9 4 7 5 12
2 -1 1
2 2 2 2
水题,就是求每个数之间的距离的最大值和最小值。
AC代码:
#include<iostream> #include<algorithm> #include<cstring> #include<cstdio> using namespace std; #define T 100100 int a[T]; int main() { int n,i,ma,mi; while(~scanf("%d",&n)) { for(i=0;i<n;++i) scanf("%d",&a[i]); printf("%d %d\n",a[1]-a[0],a[n-1]-a[0]); for(i=1;i<=n-2;++i) { ma = max(a[i]-a[0],a[n-1]-a[i]); mi = min(a[i]-a[i-1],a[i+1]-a[i]); printf("%d %d\n",mi,ma); } printf("%d %d\n",a[n-1]-a[n-2],a[n-1]-a[0]); } return 0; }