POJ 2823 Monotone Queue

Source: Internet
Author: User
poj2823 (monotone queue)//Given an array of known size and a sliding window of known size, the window moves backwards one at a time to find the maximum and minimum values for the numbers in each time window.
This question is the entry question for the monotonous queue. Max: Set up a monotonically descending queue, the elements are queued from left to right, before the queue must start deleting those elements that are smaller or equal than the current enqueued element,//until you encounter an element that is larger than the current enqueued element,//or the queue is empty. If the size of the queue exceeds the window value, the element is removed from the team header until the queue size is smaller than the value of the Yu window.
Then insert the current element into the end of the queue. Minimum value: Establish a monotone increment queue, the elements from left to right to the queue, before the team must start to delete those elements larger or equal than the current enqueued element,//until a smaller element than the current element, or the queue is empty.
If the queue size exceeds the window value, the element is deleted from the team header until the queue size is small into the window value.
Then insert the current element into the end of the queue. Set the window size of K, the method is to establish two monotone queue, first from the original array to take the first k elements in accordance with the above requirements, and then get the team head element, and then put the next element into the team,//And then get the head element, so that until the last element so far.
Finally, the answer is output from beginning to end. #include <iostream> #include <cstring> #include <string> #include <cstdio> #include <


Cstdlib> #define MAXN 1000100 using namespace std;
    struct Node {int val;
int POS;
};
Node MINQUE[MAXN],MAXQUE[MAXN];
int MINANS[MAXN],MAXANS[MAXN];
    int main () {int n,k,num; while (scanf ("%d%d", &n,&k)!=eof) {int minhead=0;int mintail=0;
    int Maxhead=0;int maxtail=0;
        for (int i=1;i<=k;i++) {scanf ("%d", &num); while ((Minhead<mintAIL) &&num<=minque[mintail-1].val) mintail--;
        Minque[mintail].val=num;
        Minque[mintail].pos=i;

        mintail++;
        while ((Maxhead<maxtail) && (num>=maxque[maxtail-1].val)) maxtail--;
        Maxque[maxtail].val=num;
        Maxque[maxtail].pos=i;
    maxtail++;
} int cur=0;
cout<<minhead<< "" <<mintail<<endl;

cout<<minque[minhead].val<< "" <<minque[1].val<<endl;
    cout<<minque[minhead].val<< "" <<maxque[maxhead].val<<endl;
        for (int i=k+1;i<=n;i++) {minans[cur]=minque[minhead].val;
        Maxans[cur]=maxque[maxhead].val;
        cur++;
        scanf ("%d", &num);
        while ((Minhead<mintail) && (i-minque[minhead].pos) >=k) minhead++;
        while ((Minhead<mintail) &&minque[mintail-1].val>=num) mintail--;
        Minque[mintail].val=num;
        Minque[mintail].pos=i;

        mintail++; WhIle ((Maxhead<maxtail) && (i-maxque[maxhead].pos) >=k) maxhead++;
        while ((Maxhead<maxtail) &&maxque[maxtail-1].val<=num) maxtail--;
        Maxque[maxtail].val=num;
        Maxque[maxtail].pos=i;
    maxtail++;
    } Minans[cur]=minque[minhead].val;
    Maxans[cur]=maxque[maxhead].val;
    cur++;
    for (int i=0;i<cur-1;i++) {cout<<minans[i]<< "";
    } cout<<minans[cur-1]<<endl;
    for (int i=0;i<cur-1;i++) {cout<<maxans[i]<< "";
} cout<<maxans[cur-1]<<endl;
} return 0;
 }

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.