"Algorithmic learning Notes" 53. Simple application of the monotone queue SJTU OJ 10,342 Brother Gold Chain

Source: Internet
Author: User

1034. Second brother's gold chain description

A sunny weekend, two elder brother out of sightseeing, but careless two elder brother on the road to lose the purse. In the evening, two elder brother came to a small hotel, he turned the whole body pocket also did not turn over how much money, and his only value is a beautiful gold chain. This gold chain exudes a singular luster, it is said to wear it can bless examination door not hanging, rp++. The good-hearted boss sympathize with the second elder brother's encounter, agreed to two elder brother with this gold chain to checkout. Although second brother very reluctant to this gold chain, but he must use it to pay for the night of the room money.

The gold chain is ring-shaped, there are N knots, the boss's asking price is K-section. Casually remove the K section of the natural no problem, but each section of the gold chain RP value is actually not the same, there is a high and low, two elder brother himself very clear. The other two elder brother does not want the whole gold chain to break up, he only is willing to cut two knives in this ring of gold chain, thus cut down a section just for K-knots gold chain to the boss. Because the higher the RP value, the more rare the section, he wants to give the boss the highest RP value on the gold chain.

Input Format

The first line of two integers n and K, indicating that the gold necklace has n knots, the boss asking for K-section.

The second line is separated by a space of n positive integer a1...an, which indicates the value of each golden chain.

Output Format

Output An integer that indicates the minimum number of RP values on the gold chain that the elder brother gave to the boss.

Sample Input
5 21 2 3 4 5
Sample Output
2
Sample Input
6 31 4 7 2 8 3
Sample Output
4


The simplest is the O (n*k) algorithm
There are a lot of repetitive comparison processes
To eliminate the repetition of the size of the problem to consider how to use monotonicity to accomplish this thing, so think of the monotonous queue.

Here to maintain a monotonous strict descending sequence can be, remember when considering the team, according to the element of the queue forward-K to find the elements of the team, compared with the team, if it happens to be, then out of the team, or not move, because it has been maintained before the monotony, so there will not be smaller than the first element in the team.
Because every element has to go through the team, so it's O (n+k-1).
Note that the circular process is simplified into an array, with space for code simplicity
#include <iostream>#include<cstdio>using namespacestd;intrp[200000+ +];//Store RPintFront =0;intRear =0;BOOLempty () {returnfront==Rear;}voidQ_out (intx) {    //because at this point X was definitely in the queue or already out of the team, if the first team is X, then out of the team or not moving.    if(que[front]==x) Front++;}voidQ_in (intx) {    //Remove all elements that are smaller than it when you queue     while(!empty ()) {        if(que[rear-1]<x) Rear--; Else             Break; } que[rear++] =x;}voidprint () { for(inti = front; I < rear; ++i) {cout<<que[i]<<" "; } cout<<Endl;}intMainintargcChar Const*argv[]) {        intN,k;//Add ring change after length of N+k-1    intAns =0; scanf ("%d%d",&n,&K);  for(inti =1; I <= N; ++i) {scanf ("%d",&Rp[i]); if(i<=k-1) {Rp[n+i] =Rp[i]; }    }    //Initialize ans     for(inti =1; I <= K; ++i) {if(Ans <Rp[i]) ans=Rp[i];    Q_in (Rp[i]); }     for(inti = k +1; I <= n+k-1; ++i) {q_out (rp[i-K]);        Q_in (Rp[i]); //print ();        if(Que[front] <ans) ans=Que[front]; } cout<<ans<<Endl; return 0;}

"Algorithmic learning Notes" 53. Simple application of the monotone queue SJTU OJ 10,342 Brother Gold Chain

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.