1. 程式人生 > >POJ 2823 Sliding Window(單調佇列入門題)

POJ 2823 Sliding Window(單調佇列入門題)

Sliding Window
Time Limit: 12000MS Memory Limit: 65536K
Total Submissions: 67218 Accepted: 19088
Case Time Limit: 5000MS

Description

An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array to the very right. You can only see the k numbers in the window. Each time 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.
Window positionMinimum valueMaximum 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 two lines. The first line contains two integers n and k which are the lengths of the array and the sliding window. There are n integers in the second line. 

Output

There are two lines in the output. The first line gives the minimum values in the window 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

Source

這個問題用RMQ或者倍增法能在o(nlogn)內解決。 但是用雙端佇列(單調佇列)能在o(n)內解決。 求最小值:建立一個單調遞增佇列,元素從左到右依次入隊,入隊之前必須從佇列發問開始刪除那些比當前入隊元素大或者相等的元素,直到遇到一個比當前入隊元素小的元素,或者佇列為空為止。若此時佇列的大小超過視窗值,則從隊頭刪除元素,直到佇列大小小入視窗值為止。然後把當前元素插入隊尾。

每滑動一次,取佇列的隊首元素作為這一區間範圍的最小值。並把超過區間範圍的最小值刪除。若有更小的值入隊,則把佇列裡其它比它大的刪了,因為這個最小值在它們後面,又比它們小,所以它們沒有存在的意義了。

#include<stdio.h>
#include<string.h>
#include<stdio.h>  
#include<string.h>  
#include<stdlib.h>  
#include<queue>  
#include<stack>  
#include<math.h>  
#include<vector>  
#include<map>  
#include<set>  
#include<cmath>  
#include<string>  
#include<algorithm>  
#include<iostream>  
#include<string.h>
#include<algorithm>
#include<vector>
#include<stdio.h>
#include<cstdio>
#include<time.h>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
int mi[1000005];
int ma[1000005];
int a[1000005];
deque<int>q;
int main()
{
    int n,k;
    scanf("%d %d",&n,&k);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    while(!q.empty ()) q.pop_back();
    for(int i=1;i<=n;i++)
    {
        while(!q.empty ()&&a[q.back ()]>=a[i]) q.pop_back();
        if(q.empty ()) mi[i]=i;
        else mi[i]=q.front ();
        q.push_back (i);
        if(q.front ()==i-k+1) q.pop_front ();
    }

    while(!q.empty ()) q.pop_back();
    for(int i=1;i<=n;i++)
    {
        while(!q.empty ()&&a[q.back ()]<=a[i]) q.pop_back();
        if(q.empty ()) ma[i]=i;
        else ma[i]=q.front ();
        q.push_back (i);
        if(q.front ()==i-k+1) q.pop_front ();
    }

    for(int i=k;i<=n;i++)
    {
        printf("%d",a[mi[i]]);
        if(i!=n) printf(" ");
    }
    printf("\n");
    for(int i=k;i<=n;i++)
    {
        printf("%d",a[ma[i]]);
        if(i!=n) printf(" ");
    }
    return 0;
}

相關推薦

POJ 2823 Sliding Window單調佇列入門

Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 67218 Accepted: 19088 Case Time Limit: 5000MS Description An array of size 

POJ 2823 Sliding Window單調佇列||線段樹

題意: 讓你不停的查詢區間長度為k的最小值和最大值 一開始用set模擬,發現常熟太大,T了,然後開始學單調佇列,看了一會兒還是好理解的,所以就寫了,發現G++會T,只能交C++,後來發現 有人線段樹也能過,於是就寫了一發,還真行,只不過就是吧

POJ 2823 Sliding Window單調佇列 || 線段樹題解

題意:求每個長度為k的陣列的最大值和最小值思路:1.用線段樹建立維護最大值和最小值,遍歷詢問,簡單複習了一下...有點手生2.單調佇列:可以看一下詳解單調佇列顧名思義就是一個單調遞增或者遞減的佇列,我們可以通過佇列瞬間得到當前佇列的最大值和最小值。以查詢當前區間最小值為例,我

POJ-2823-Sliding Window 單調佇列

原題連結: http://poj.org/problem?id=2823 An array of size n ≤ 10 6 is given to you. There is a sliding window of size k which is moving from the ver

POJ 2823 Sliding Window 單調佇列@

An array of size n ≤ 10 6 is given to you. There is a sliding window of size kwhich is moving from the very left of the array to the ve

poj 2823 Sliding Window單調佇列模板

介紹單調佇列不錯的部落格:點選開啟連結 還有這題輸出用printf G++交會T,C++就能過.....而用cout兩個都能過..好坑.... 程式碼: #include<cstdio> using namespace std; const int maxn

POJ - 2823 Sliding Window單調佇列優化dp && c++快讀】

Sliding Window Time Limit: 12000MS   Memory Limit: 65536K Total Submissions: 72718 &nb

Poj 2823 Sliding Window單調佇列學習】模板記錄

Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 62889 Accepted: 17951 Case Time Limit: 5000MS Descripti

[poj2823]sliding windows單調佇列模板

DescriptionAn array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the very left of the array t

POJ 2823 Sliding Window經典單調佇列

An array of size n ≤ 10 6 is given to you. There is a sliding window of size kwhich is moving from the very left of the array to the ve

POJ 2823Sliding Window單調佇列

Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 56138 Accepted: 1613

2823 Sliding Window單調佇列優化dp && c++快讀】

Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 72718 Acc

2559單調入門

A histogram is a polygon composed of a sequence of rectangles aligned at a common base line. The rectangles have equal widths but may have different heig

單調佇列 POJ 2823 Sliding Window

  Sliding Window An array of size n ≤ 10 6 is given to you. There is a sliding window of size kwhich is moving from

poj 2823 單調佇列入門內含手寫佇列的學習和模板

Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 39011 Accepted: 11554 Case Time Limit: 5000MS Descripti

POJ 2823 Sliding Window (單調佇列)

題目連結 Description An array of size n ≤ 106 is given to you. There is a sliding window of size k which is moving from the v

POJ 2823 Sliding Window(單調佇列)

題意  長度為n的陣列上有個長度為k的滑窗從左向右移動  求每次移動後滑窗區間的最小值和最大值  輸出兩行  第一行所有最小值  第二行所有最大值 可以用線段樹來做  但是單調佇列更簡單  單調遞增佇列: 隊尾單調入隊(入隊元素大於隊尾元素時直接入隊  否則隊尾出隊直到隊尾

洛谷P1886 滑動窗口POJ.2823 Sliding Window區間最值

最大 ide dma include names target org void blog To 洛谷.1886 滑動窗口 To POJ.2823 Sliding Window 題目描述 現在有一堆數字共N個數字(N<=10^6),以及一個大小為k的窗口。現在這個

POJ 2823 Sliding Window? (模板)【單調隊列】

返回 一個 color www. 新元素 arc 維護 記錄 一段 <題目鏈接> <轉載於>>> > 題目大意: 給你一段序列和一個長為k的窗口,這個窗口從最左邊逐漸向右滑,直到滑到最右邊,問你,該窗口在滑動的過程中,最大值和最小值

POJ 2823 Sliding Window【RMQ壓縮長度確定

題意:給一個長度為N的陣列,輸出所有區間長度為K的陣列元素的最大值與最小值; AC程式碼: #include<cstdio> #include<algorithm> u