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
inputCopy
4
-5 -2 2 7
outputCopy
3 12
3 9
4 7
5 12
inputCopy
2
-1 1
outputCopy
2 2
2 2
这道题的测试数据TQL!!!!!
不能暴力,要想一下如何快速求解。
思路;确定一个最小值 要和最近的两个数比较
确定一个最大值,要和两头的数比较。

///***Created by Xiang wang *** ACMÈÙҫ֮·***
//#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define ll long long
const int  INF = 0x3f3f3f3f;
const ll LIMIT = 4294967295LL;
const ll maxn = -1;
const ll minn = 10000000010;
using namespace std;
ll a[1000100], b[1000100];
int main() {
    ll n;
    ll i, j;
    ll maxn = -1;
    ll minn = 10000000010;
    //8 4 2 6 8 4//确定一个最小值  要和最近的两个数比较
    //确定一个最大值,要和两头的数比较。
    while(cin >> n ) {
        for(i = 0; i < n; i++)
            cin >> a[i];
        for(j = 0; j < n; j++) {
            if(j == 0) {
                maxn = abs(a[n - 1] - a[j]);
                minn = abs(a[1] - a[j]);
            } else if(j == n - 1) {
                maxn = abs(a[0] - a[n - 1]);
                minn = abs(a[n - 2] - a[n - 1]);
            } else {
                maxn = abs(a[0] - a[j]) > abs(a[n - 1] - a[j]) ? abs(a[0] - a[j]) : abs(a[n - 1] - a[j]);
                minn = abs(a[j - 1] - a[j]) > abs(a[j + 1] - a[j]) ? abs(a[j + 1] - a[j]) : abs(a[j - 1] - a[j]);
            }
            cout << minn << ' ' << maxn << endl;
        }
    }
    return 0;
}




你可能感兴趣的:(A. Lineland Mail 思维题)