P1886 sliding window, p1886 Sliding Window

Source: Internet
Author: User

P1886 sliding window, p1886 Sliding Window
Description

There are now a bunch of numbers with N numbers (N <= 10 ^ 6) and a window in k size. Now this slide from the left to the right, sliding one unit each time, find the maximum and minimum values in the window after each sliding.

For example:

The array is [1 3-1-3 5 3 6 7], and k = 3.

Input/Output Format

Input Format:

 

There are two rows in the input. The first row is n, k.

Number of the second behavior n (<INT_MAX ).

 

Output Format:

 

The output contains two rows. The first row is the minimum value of every window sliding.

The second action is the maximum sliding value of each window.

 

Input and Output sample input sample #1:
8 31 3 -1 -3 5 3 6 7
Output sample #1:
-1 -3 -3 -3 3 33 3 5 5 6 7
Description

50% of data, n <= 10 ^ 5

100% of data, n <= 10 ^ 6

 

 

I feel that I am writing code more and more streamlined.

Stl provides a double-end queue called deque.

This type of queue can be inserted or deleted at the beginning and end of the queue.

This solves the problem that queue does not need to be applied to monotonous queues.

Deque usage.

A picture is sufficient for overview

 

For this question. Querying the maximum and minimum values is actually a matter of modifying the queuing conditions,

Solution to a three-object Operator

 

#include<iostream>#include<cstdio>#include<deque>using namespace std;const int MAXN=2000050;const int maxn=0x7fffffff;void read(int &n){char c='+';int x=0;bool flag=0;while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;}while(c>='0'&&c<='9'){x=x*10+(c-48);c=getchar();}flag==1?n=-x:n=x;}int n,k;int a[MAXN];struct node{int w,p;node (int a,int b){w=a;p=b;}};deque<node>q;void find(bool how){while(q.size()) q.pop_front();for(int i=1;i<=n;i++){while(q.size()!=0&&(how==0?(q.back().w<a[i]):(q.back().w>a[i])))q.pop_back();q.push_back(node(a[i],i)); while(q.front().p<=(i-k))q.pop_front();if(i>=k)printf("%d ",q.front().w);}printf("\n");}int main(){    read(n);read(k);    for(int i=1;i<=n;i++)read(a[i]);    find(1);find(0);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.