#P3453. [POI2007] DRZ-Trees

[POI2007] DRZ-Trees

题目描述

Byteasar has a cottage. Lately, he has bought nn trees and had them planted all in one row. Byteasar does not, however, like the order which the trees have been planted in. It particularly annoys him that tall and short ones have been mixed up, and the composition does not meet his aesthetic criteria.

Byteasar has invented a disorder coefficient so as to allow the gardener to comprehend his intentions: thelower the value of the coefficient the prettier the row of trees. It is defined in the following way:

h1h2+h2h3++hn1hn|h_1-h_2|+|h_2-h_3|+\cdots+|h_{n-1}-h_n|, where h1,h2,,hnh_1,h_2,\cdots,h_n are the heights of consecutive trees in a row. Replanting is a very toilsome and cumbersome task, therefore Byteasar has ordered the gardener to replanttwo trees at the most (i.e. interchange their positions). The task of the gardener is to choose the pair to replantin a way that makes the disorder coefficient the smallest.

The gardener is not sure if he has chosen the correct pair of trees and he fears he may lose his job ifhe is mistaken. Help him: for each tree calculate the minimal disorder coefficient that may be attained byswitching places with any other tree.

TaskWrite a programme which:

reads the height of the consecutive trees in a row from the standard input, for each tree calculates the minimal disorder coefficient that may be attained should it switch places with some other tree (or should there be no change at all), writes the outcome to the standard output.

定义一排树的不整齐程度为相邻两树的高度差的和。设树高分别为 h1,h2,,hnh _ 1, h _ 2, \cdots, h _ n,那么不整齐程度定义为:h1h2+h2h3++hn1hn|h_1-h_2|+|h_2-h_3|+\cdots+|h_{n-1}-h_n|

请对于每个 ii 求出,如果只把 ii 和另一棵树交换高度(可以不交换),那么不整齐度最小是多少。

输入格式

The first line of the standard input contains one integer nn (2n50 0002\le n\le 50\ 000).

The other contains nn integers hih_i (1hi100 000 0001\le h_i\le 100\ 000\ 000)separated by single spaces, denoting the height of the consecutive trees in the row.

输出格式

The output should consist of precisely nn lines. The ii'th line should contain a single integer - the smallestdisorder coefficient attainable when considering replanting of the ii'th tree.

5
7 4 5 2 5
7
7
8
7
7