POJ 2823 (Get started with the classic sliding window max problem monotone queue)

Source: Internet
Author: User
Tags min printf
Topic links

POJ 2823 Topic

Enter a sequence of n (n≤\le 106 10^6), given a window with a length of K, to move the window over the number of columns, moving to the maximum and minimum values for each location window. The sequence is set to A1 a_1, A2 a_2,..., an a_n, set F (i) =min{ai−k+1 a_{i-k+1}, ai−k+2 a_{i-k+2},..., Ak A_k}, G (i) =max{ai−k+1 a_{i-k+1}, ai−k+ 2 a_{i-k+2},..., Ak A_k}
: F (k), F (k+1),..., f (n) g (k), G (k+1),..., g (n). Analysis

This is a classic entry question for a monotonous queue. So take it to learn the monotonous queue.
If you simply scan each of the first k numbers to find the maximum, then the complexity is O (n*k), definitely timeout.
If you maintain interval maximum with a line segment tree or a tree array, the complexity is O (NLOGN).
But there is a more concise and efficient way to do that, namely, a monotone queue . can refer to purple book P241
A monotonic queue is a special queue that can delete operations at both ends of a queue and always maintains a monotonic queue.
For example, to minimize the window, we need to maintain a queue of internal element values to simulate sliding windows so that each query takes the first element of the team as its answer. How to maintain it. There are two ways to do this:

1. Insert elements from the end of the team: when a new element needs to be queued, let it be compared with the current team tail element, if it is less than the current team tail element (that is, it destroys the monotony of the original queue), then delete the tail element, and continue to compare the tail with the new element until a team tail is greater than the new The deleted element is larger than the new element and slides out of the window before the new element, so it certainly won't be an answer. This operation continuously maintains the maximum value in the queue.

2. Delete the first element of the team: because the elements in the sequence are only valid when the window is sliding, the first element of the team is deleted when the first element is not in the sliding window. This operation maintains the temporal nature of the data.

Whether an element is obsolete is related to its subscript, so my queue stores the subscript for the sequence number, so that whenever you want to enqueue a number, you only need to access the number of the first element stored in the sequence to get the answer.
This is done although it is sometimes necessary to delete multiple numbers when inserting elements, but each number is deleted at most once, so the time complexity is O (n). Code

Array Simulation monotone queue

#include <iostream> #include <cstdio> #include <cstring> #define MAXN 1000010 using namespace std;
int a[maxn],min[maxn],max[maxn],q[maxn],n,k;
    void Get_min () {int head,tail,i;
    memset (q,0,sizeof (Q));
    head=1;tail=1;
    Q[tail]=1;
    MIN[1]=A[1];                            for (i=2;i<=n;i++) {while ((Head<=tail) && (A[i]<a[q[tail]))//delete the tail element tail--;
        Insert tail element q[++tail]=i;
        while ((Head<=tail) && (q[head]<i-k+1))//delete team first OBSOLETE element head++;                         /* Because each layer of the loop only adds one number, so the first time to delete a team head, it can be changed to the IF statement */Min[i]=a[q[head]];
    Access answer}} void Get_max () {int head,tail,i;
    memset (Q,sizeof (Q), 0);
    head=1;tail=1;
    Q[tail]=1;
    MAX[1]=A[1];
        for (i=2;i<=n;i++) {while ((Head<=tail) && (A[i]>a[q[tail])) tail--;
        Q[++tail]=i; while ((Head<=tail) && (q[head]<i-k+1)) Head++;
    Max[i]=a[q[head]];
    }} int main () {int i;
    scanf ("%d%d", &n,&k);
    for (i=1;i<=n;i++) scanf ("%d", &a[i]);
    Get_min ();

    Get_max ();
    for (i=k;i<n;i++) printf ("%d", min[i]);
    printf ("%d\n", Min[n]);
    for (i=k;i<n;i++) printf ("%d", max[i]);
    printf ("%d\n", Max[n]);
return 0;
 }

Using a double-ended queue deque container in STL

#include <iostream> #include <cstdio> #include <cstring> #include <deque> #define MAXN 1000010 US
ing namespace std;
int a[maxn],min[maxn],max[maxn],q[maxn],n,k;
    void Get_min () {deque<int> Q;
    int i;
    Q.push_back (1);
    MIN[1]=A[1]; for (i=2;i<=n;i++) {while (!
        Q.empty ()) && (A[i]<a[q.back ())) Q.pop_back ();
        Q.push_back (i); while ((!
        Q.empty ()) && (Q.front () <i-k+1)) Q.pop_front ();
    Min[i]=a[q.front ()];
    }} void Get_max () {deque<int> Q;
    int i;
    Q.push_back (1);
    MAX[1]=A[1]; for (i=2;i<=n;i++) {while (!
        Q.empty ()) && (A[i]>a[q.back ())) Q.pop_back ();
        Q.push_back (i); while ((!
        Q.empty ()) && (Q.front () <i-k+1)) Q.pop_front ();
    Max[i]=a[q.front ()];
    }} int main () {int i;
    scanf ("%d%d", &n,&k); for (i=1;i<=n;i++) scanf ("%d",&A[i]);
    Get_min ();

    Get_max ();
    for (i=k;i<n;i++) printf ("%d", min[i]);
    printf ("%d\n", Min[n]);
    for (i=k;i<n;i++) printf ("%d", max[i]);
    printf ("%d\n", Max[n]);
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.