Black Book 1.2.6: discrete mathematics

Source: Internet
Author: User

 
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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.