# Application of the ruler extraction

Source: Internet
Author: User

Ruler method is actually a very simple algorithm, thought has been used, but has not known. These two days happened to do two questions about the ruler, just to summarize.

First, what is a ruler? The ruler method usually refers to the array to save a pair of subscript (starting point, focus), and then according to the actual situation of alternating two end points to arrive at the answer, because this method like the crawling way of the worm is named. In fact, it is to ask for an interval problem on a linear array. Because only the two endpoints of the interval are changed, the middle part does not need maintenance updates, as long as the two ends can be maintained. Can greatly reduce the complexity.

Example one: poj3061 given an integer sequence of length n and an integer s, the minimum value of the length of a continuous subsequence with a sum not less than S is obtained, if the solution does not exist, the output is 0.

Solution One: (1) s=t=sum=0 initialization

(2) As long as the sum<s, will continue to increase the sum of a (t), and add T 1

(3) If the sum>=s is not met in (2), then the ANS is updated to min (ans,t-s).

(4) The sum is subtracted by a (s), and S is increased by 1 to return to (2)

This is actually a typical ruler, when we get sum>=s and then subtract a (s) it will be less than S is equivalent to re-determine the starting point, the same, but also to determine the end point, and because the new area and the original interval has a large part overlap, so directly to sum to add and subtract on it, No recalculation is required.

Example two: Give a string of beads, there are 2n, of which there are black and white beads each N, black counter-clockwise number, white clockwise numbering, white black random arrangement, to find a length of the interval of n, requires the number of beads within the range of 1-n (not necessarily from small to large or from large to small appearance, as long as 1-n can be) (Thank God for the quiz!) ) data range 10 million.

Summary: Overall ruler method of the idea is very good understanding, code difficulty is not high, in the NOIP exam is suitable, some problems can be convenient to solve, personal feeling should master.

Application of the ruler extraction

Related Keywords:

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.