Sliding windows (POJ 2823)

Source: Internet
Author: User

"Problem description"

Give you an array of length n, a sliding form of K is moved from the leftmost to the far right, you can only see the number of K in the window, each time the form is moved to the right, such as the following table:

"Sample Input"

8 3

1 3-1-3 5 3 6 7

"Sample Output"

-1-3 -3-3 3 3

3 3 5 5 6 7

"Problem-solving ideas"

first, it is not difficult to think of the enumeration method, for each interval, the enumeration of the maximum and minimum values, but for n<=1000000 data, the enumeration must time out, so we can use the queue to optimize.

We take the maximum and the minimum of the first k number, put in two queues, set the maximum value of K, put the minimum value of Q, for each subsequent number if more than the team tail element in K, then K of the team tail elements can be deleted (because it can be proved that in any subsequent interval is not used in the element), if less than the tail element , they joined the queue. The Q queue is an inverse operation. So, for the minimum and maximum of each interval, we only need to record the first element in the Q and K queues. Because of the requirements of the output I used two procedures here, the minimum value first, and then the maximum value. See the code.

"Code Implementation"
varQ,a:Array[1..1000000] ofLongint; N,k,i,j:longint;procedureWork ;varI,f,r:longint;beginF:=1; R:=1; Q[f]:=1;  fori:=2  toK Do  begin    while(F&LT;=R) and(A[q[r]]>=a[i]) DoDec (r);   Inc (R); Q[R]:=i; End;//Initialize Q queue write (A[q[f]],' ');  fori:=2  ton-k+1  Do  begin   ifQ[f]<i ThenInc (F);  while(F&LT;=R) and(a[q[r]]>=a[i+k-1]) Do//Delete the tail element Dec (r) If the current element is smaller than the team tail element;   Inc (R); Q[R]:=i+k-1; Write (A[q[f]],' '); End;End;procedureWork1;varI,f,r:longint;beginF:=1; R:=1; Q[f]:=1;  fori:=2  toK Do  begin    while(F&LT;=R) and(A[q[r]]<=a[i]) DoDec (r);   Inc (R); Q[R]:=i; End; write (a[q[f]),' ');//Initialize K queue fori:=2  ton-k+1  Do  begin   ifQ[f]<i ThenInc (F);  while(F&LT;=R) and(a[q[r]]<=a[i+k-1]) Do//If the current number is larger than the tail element, delete the team tail element Dec (r);   Inc (R); Q[R]:=i+k-1; Write (A[q[f]],' '); End;End;beginreadln (n,k); fori:=1  toN Doread (a[i]); Fillchar (q,sizeof (q),0); Writeln; Work1; Writeln;End.

Sliding windows (POJ 2823)

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.