567A - Lineland Mail

A. Lineland Mail
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
All cities of Lineland are located on the Ox coordinate axis. Thus, each city is associated with its position xi — a coordinate on the Ox axis. 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
Input
The first line of the input contains integer n (2 ≤ n ≤ 105) — the number of cities in Lineland. The second line contains the sequence of n 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 in ascending order.
Output
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.
Examples
Input
Copy
4
-5 -2 2 7
Output
Copy
3 12
3 9
4 7
5 12
Input
Copy
2
-1 1
Output
Copy
2 2
2 2
题解:把坐标排序,注意第一个元素和最后一个元素。最小有两种可能,与前面的差或与后面的差。max也有两种可能,与第一个元素或最后一个元素的差。

#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
 int n,i,j,k,min1,min,max1,max;
 int a[100001]={0};
 cin>>n;
 for(i=0;i<n;i++)
 {
  cin>>a[i];
 }
 sort(a,a+n);
  min=a[1]-a[0];
  max=a[n-1]-a[0];
  cout<<min<<" "<<max<<'\n';
  for(i=1;i<n-1;i++)
  {
   min=a[i]-a[i-1];
   min1=a[i+1]-a[i];
   if(min<min1)
   {
    cout<<min<<" ";
   }
   else
   {
    cout<<min1<<" ";
   }
   max=a[i]-a[0];
   max1=a[n-1]-a[i]; 
   if(max>max1)
   {
    cout<<max<<'\n';
   }
   else
   {
    cout<<max1<<'\n';
   }
  }
  min = a[n-1]-a[n-2];
  max = a[n-1]-a[0];
  cout<<min<< " "<<max<<'\n';
 return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值