Code Forces 567 A. Lineland Mail(水~)

Description
x轴上有一些点,现在按顺序给出这些点的位置,输出每个点其他点的最近距离和最远距离
Input
第一行为一整数n表示点的个数,第二行n个整数表示点的位置
Output
输出包括n行,每行两个整数分别表示该点到其他点的最近和最远距离
Sample Input
4
-5 -2 2 7
Sample Output
3 12
3 9
4 7
5 12
Solution
每个点距其他点的最近距离显然为它到它左右两点距离的最小值,距其他点的最大距离显然为它到两端点的距离最大值
Code

#include<cstdio>
#include<iostream>
using namespace std;
#define maxn 111111
int n;
int d[maxn];
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=1;i<=n;i++)
            scanf("%d",&d[i]);
        printf("%d %d\n",d[2]-d[1],d[n]-d[1]);
        for(int i=2;i<n;i++)
        {
            int ans1=min(d[i]-d[i-1],d[i+1]-d[i]);
            int ans2=max(d[i]-d[1],d[n]-d[i]);
            printf("%d %d\n",ans1,ans2);
        }
        printf("%d %d\n",d[n]-d[n-1],d[n]-d[1]);
    }
    return 0;
} 

你可能感兴趣的:(Code Forces 567 A. Lineland Mail(水~))