Cdoj Sliding Window Segment Tree (NLOGN) or double-ended queue (n) template

Source: Internet
Author: User

Topic Links:l-sliding WindowTime limit:6000MS Memory Limit:131072KB 64bit IO Format:%lld &%llu Submit Status Practice UESTC 201 Appoint Description:System Crawler (2016-04-24)

Description

An array of sizeN≤6 is given to you . There is a sliding window of size K which was moving from the very left of the array to the very right. You can only see the K numbers in the window. Each of the sliding window moves rightwards by one position. Following is an example:

The array is[1,3,−1,−3,5,3,6,7 ], And k is 3< Span id= "Mathjax-element-6-frame" class= "Mathjax mathjax_processing". Window position Minimum Value Maximum value

| Window Position | Minimum value | Maximum value |
| ———————— |: ——————— –:|: ———————: |
|[1,3,−1],−3,5,3,6,7|−1|3
|1,[3,−1,−3],5,3,6,7|−3|3
|1,3,[−1,−3,5],3,6,7|−3|5
|1,3,−1,[−3,5,3],6,7|−3|5
|1,3,−1,−3,[5,3,6],7|3|6
|1,3,−1,−3,5,[3,6, 7] | 3 |  7

Your task is to determine the maximum and minimum values in the sliding window at each position.

Input

the input consists of the lines. The first line contains the Integers  n and k  which is the lengths of the array and The sliding window. There Are n  Integers in the second line.

Output

There is lines in the output. The first line gives the minimum values in the windows at each position, from left to right, respectively. The second line gives the maximum values.

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

Hint

The data used in this problem are unofficial data prepared by love8909. So any mistake this does not imply mistake in the offcial judge data.

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.

#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath > #include <vector> #include <queue> #include <stack> #include <map> #include <algorithm > #include <set>using namespace std;typedef long long ll;typedef unsigned long long Ull; #define MM (A, B) memset (A, B , sizeof (a)); const double EPS = 1e-10;const int inf = 0x3f3f3f3f;const double Pi=acos ( -1); const int Maxn=1000000;int A[MAXN    +10],deq[maxn+10],ans[maxn+10],n,k;void minq () {int s=1,t=0;        for (int i=1;i<=n;i++) {while (S<=t&&a[deq[t]]>a[i]) t--;//This place does not add equal to the seemingly all can deq[++t]=i;            if (i>=k) {ans[i-k+1]=a[deq[s]];        if (deq[s]==i-k+1) s++;    }} for (int i=1;i<n-k+1;i++) printf ("%d", ans[i]); printf ("%d\n", Ans[n-k+1]);}    void MaxQ () {int s=1,t=0;        for (int i=1;i<=n;i++) {while (s<=t&&a[deq[t]]<a[i]) t--; deq[++T]=i;            if (i>=k) {ans[i-k+1]=a[deq[s]];        if (deq[s]==i-k+1) s++;    }} for (int i=1;i<n-k+1;i++) printf ("%d", ans[i]); printf ("%d\n", Ans[n-k+1]);}         int main () {while (~SCANF ("%d%d", &n,&k)} {for (int i=1;i<=n;i++) scanf ("%d", &a[i]);        Minq ();    MAXQ (); } return 0;}

  

Cdoj Sliding Window Segment Tree (NLOGN) or double-ended queue (n) template

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.