Poj 2823 Sliding Window monotonous queue

Source: Internet
Author: User

The meaning of this question is given a long N integer sequence, with a K-sized window from the beginning to overwrite, ask 1st-the maximum number and the smallest number in the N-K window.
At the beginning, I thought that the priority queue could be used to delete the first element. We can also solve this problem by using the line segment tree. I learned the monotonous queue with this question.

A monotonous queue, just like its name, is a Sort queue from small to large, and can ensure that all elements are in the queue once and out, so the complexity of each element is equal.
It is O (1 ).
Use of monotonous queue for this question. Take sequence 1 3-1-3 5 3 6 7 as an example.
1) element type: a struct that contains numbers, such as () and ).
2) insert operation: start searching from the end of the team, delete all elements at the end of the team less than the elements to be inserted, and then add the elements to be inserted. This operation is the worst
In this case, it is O (n), but we use the clustering analysis method to know that each element can be deleted once at most, then N elements are deleted N times, evenly distributed to each time
The operation complexity is O (1.
3) Delete the first element of the team. For example, if the question given in this article moves backwards, an element will be deleted for each move.
The deleted element, so each time the elements in the window are moved, a check should be performed. If the first element of the line is invalid, the first element of the line is deleted.

Code implementation. I packaged deque to implement a template class. The speed was very bad. I ran more than 11 S. Thanks to the 12 s time, the status was more than 500 ms.
That's it. It is estimated that array implementation will be much faster.

The Code is as follows:
# Include <stdio. h>
# Include <deque>
# Include <algorithm>
Using namespace std;
# Define MAX_N (1000000 + 100)
Int nNum [MAX_N];
Int nN, nK;

Struct Small
{
Int nValue;
Int nIndex;
Small (int nV, int index): nValue (nV), nIndex (index ){}
Bool operator <(const Small & a) const
{
Return nValue <a. nValue;
}
};

Struct Big
{
Int nValue;
Int nIndex;
Big (int nV, int index): nValue (nV), nIndex (index ){}
Bool operator <(const Big & a) const
{
Return nValue> a. nValue;
}
};

// Monotonous queue
Template <typename T> class Monoque
{
Deque <T> dn;

Public:
Void Insert (T node)
{
Int nPos = dn. size ()-1;
While (nPos> = 0 & node <dn [nPos])
{
-- NPos;
Dn. pop_back ();
}
Dn. push_back (node );
}

Int Top ()
{
Return dn. front (). nValue;
}

Void Del (int nBeg, int nEnd)
{
If (dn. size ()> 0)
{
If (dn. front (). nIndex <nBeg | dn. front (). nIndex> nEnd)
{
Dn. pop_front ();
}
}
}
};

Int main ()
{
While (scanf ("% d", & nN, & nK) = 2)
{
Int I;
For (I = 0; I <nN; ++ I)
{
Scanf ("% d", & nNum [I]);
}
Monoque <Small> minQ;
Monoque <Big> maxQ;

For (I = 0; I <nK; ++ I)
{
MinQ. Insert (Small (nNum [I], I ));
}

For (I = 0; I <nN-nK; ++ I)
{
Printf ("% d", minQ. Top ());
MinQ. Insert (Small (nNum [I + nK], I + nK ));
MinQ. Del (I + 1, I + nK );
} Www.2cto.com
Printf ("% d \ n", minQ. Top ());

For (I = 0; I <nK; ++ I)
{
MaxQ. Insert (Big (nNum [I], I ));
}

For (I = 0; I <nN-nK; ++ I)
{
Printf ("% d", maxQ. Top ());
MaxQ. Insert (Big (nNum [I + nK], I + nK ));
MaxQ. Del (I + 1, I + nK );
}
Printf ("% d \ n", maxQ. Top ());
}

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.