Learning notes: Monotone queue

Source: Internet
Author: User

Transferred from: http://apps.hi.baidu.com/share/detail/34010558

The monotone queue solves the problem of the maximum value of an interval segment in a sequence, and we can use the monotone queue to solve it.
For example, poj2823 Sliding Window is a good example: given a sequence, the maximum and minimum values in a range of fixed length k in a sequence are required.
The "principle" monotone queue maintains the maximum interval:
1and maintenance of the maximum value:
For example, we want to maintain a monotone queue with a maximum interval of k, because the newly inserted node his "vitality" is certainly longer than the element that was already in the queue, will be inserted element continuously with the team tail element, if he is larger than the tail element, then r--the tail element to delete,(because the currently inserted value of this element (set to POS) is larger, and "live" time is long, there is POS, the tail element of the "life" of the year will never be the maximum value, so directly ignoring the lower end of the queue than the POS) . Until the empty position or find a larger than the point of the end of the queue.
2, the maintenance of K-zone:
For example, the starting point of the K interval is I, that is, the interval is [i,i+k-1], then if the team head element front subscript j<i, then the front will not meet in the current K range, then his value is not the current K range of the maximum value, so f++ Will head out of the team. This operation will never encounter a team empty situation, should be a 1th step has been inserted in the interval for [i,i+k-1] element Pos, he subscript J must conform to J>=i


"Template Code"

1 struct nodes   2  3     int4 5 6 7  int

Descending queue:

1 voidInsertintMintIdintL//L is the leftmost subscript of the interval, m the value to insert, the index of the value of the ID2 { 3      while(r1>=f1&&m>qu1[r1].val)4r1--; 5Qu1[++r1].val=m;6qu1[r1].beg=ID;7      while(qu1[f1].beg<L)8f1++; 9 } Ten  One //f>r qu Empty AInit:f=r=0;  -  -Insert (A[I],I,L);

Increment to make the corresponding changes can be


Summarize:

The monotonous queue uses the changeable, in the concrete question, should use flexibly ~

Learning notes: Monotone queue

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.