FJUT-1370

Problem Description

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 ≤ 10^5) — the number of cities in Lineland. The second line contains the sequence of n distinct integers x1, x2, ..., xn ( - 10^9 ≤ xi ≤ 10^9), 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.

SampleInput 1
4
-5 -2 2 7

SampleOutput 1
3 12
3 9
4 7
5 12

SampleInput 2
2
-1 1

SampleOutput 2
2 2
2 2

翻译(机翻)
问题描述
Lineland的所有城市都位于Ox坐标轴上。因此,每个城市都与它的位置——Ox轴上的坐标相关联。没有两个城市位于同一地点。
利兰居民喜欢互相写信。只有在收件人住在另一个城市的情况下,一个人才可以寄信(因为如果他们住在同一个城市,那么顺道来就更容易了)。
奇怪但真实的是,寄信的费用正好等于寄信人所在城市和收件人所在城市之间的距离。
对于每个城市,计算两个值mini和maxi,其中mini是第i个城市向其他城市发送信件的最小成本,maxi是第i个城市向其他城市发送信件的最大成本

输入
输入的第一行包含整数n(2 ≤ n ≤ 10^5)-线路中的城市数。第二行包含n个不同整数的序列x1,x2,…,xn( - 10^9 ≤ xi ≤ 10^9),其中席是第i个城市的X坐标。所有的席是清晰的,遵循递增的顺序。

输出
打印n行时,第i行必须包含两个整数mini, maxi,用空格分隔,其中mini是从第i个城市发送信件的最小成本,maxi是从第i个城市发送信件的最大成本。

样本输入1
4
-5 -2 2 7

样本输出1
3 12
3 9
4 7
5 12

样本输入2
2
-1 1

样本输出2
2 2
2 2

#include<iostream>
#include<algorithm>
using namespace std;
#define T 100100
int a[T];//用于存x坐标
int main()
{
    int n,i,ma,mi;
    while(cin>>n){
        for(i=0;i<n;++i)
        cin>>a[i];
        cout<<a[1]-a[0]<<' '<<a[n-1]-a[0]<<endl;//第一个城市的最小和最大费用
        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]);//反之,最小费用则是它相邻城市的距离中最小的那个
            cout<<mi<<' '<<ma<<endl;
        }
        cout<<a[n-1]-a[n-2]<<' '<<a[n-1]-a[0]<<endl;//最后一个城市的最小和最大费用
    }
    return 0;
}

多思考,少使用循环,易超时。

全部评论

相关推荐

2 收藏 评论
分享
牛客网
牛客企业服务