Description: a function is defined in the following fields:1To N (2<= N <=100000), Value range F (n) is longInt(InC ++) (the final proof is in-2^31-2^31). The two vertices A and B on the function are required to make the f function of X ε (A, B) below the straight line AB, and the AB inclination is required to be the largest.
According to the black book, The O (N ^ 2) algorithm is indeed very easy to produce, but there is an O (n) algorithm. After thinking for a long time, I cannot understand it. I did think of the Final Solution in the middle, but did not think about it carefully. Therefore, I did not prove its correctness and finally did not get along with this question.
Write the algorithm according to the same person on the Internet:
From left to right scan, O (n) time, calculate the slope between two adjacent points, and finally select a pair of the maximum slope as the expected result.
The proof of algorithm correctness is simple:
1. The line between adjacent highlights must satisfy the condition that any intermediate point is below the straight line.
2. The greatest inclination is first understood as the meaning of the greatest slope. It can be proved that as long as there is a straight line meeting condition 1, to maximize its slope, it must exist between two adjacent points. Inverse Method: if there is a straight line with the largest inclination angle, but there are other points below the straight line in the middle, then a straight line with a larger inclination will certainly exist, it is assumed that the angle of the link between the point below the straight line and the point at one end is greater.
The above proof shows that the required result must exist between two adjacent points.