1. 程式人生 > >Poj 2823 Sliding Window【單調佇列學習】模板記錄

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

Sliding Window
Time Limit: 12000MS Memory Limit: 65536K
Total Submissions: 62889 Accepted: 17951
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 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 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問題,這裡溫習(重新學習)一下單調佇列。

單調佇列基礎操作:

①出隊,我們首先將老元素從隊頭依次出隊。

②入隊,我們從後向前掃,找到第一個小於(大於)當前入隊的元素之後,將當前元素放到這個位子後邊,同時刪除所有後邊不必要存在元素。

那麼對應這個題,我們出隊之後入隊然後查詢即可。

Ac程式碼:

#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
struct node
{
    int val,pos;
} q[3500000];
int a[3500000];
int ans[3500000];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=1; i<=n; i++)scanf("%d",&a[i]);
        int s=0,e=-1;
        for(int i=1; i<=n; i++)
        {
            while(s<=e&&q[s].pos<i-m+1)s++;
            while(s<=e&&q[e].val>a[i])e--;
            e++,q[e].pos=i,q[e].val=a[i];
            if(i>=m)ans[i]=q[s].val;
        }
        for(int i=m; i<=n; i++)
        {
            if(i>m)printf(" ");
            printf("%d",ans[i]);
        }
        printf("\n");
        s=0,e=-1;
        for(int i=1; i<=n; i++)
        {
            while(s<=e&&q[s].pos<i-m+1)s++;
            while(s<=e&&q[e].val<a[i])e--;
            e++,q[e].pos=i,q[e].val=a[i];
            if(i>=m)ans[i]=q[s].val;
        }
        for(int i=m; i<=n; i++)
        {
            if(i>m)printf(" ");
            printf("%d",ans[i]);
        }
        printf("\n");
    }
    return 0;
}


相關推薦

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

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

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

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

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

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

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

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

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

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單調佇列)@

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單調佇列 || 線段樹)題解

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

POJ2823 Sliding Window單調佇列線段樹ST表

Sliding Window 題意: 給出一個長度為N的序列,通過一個視窗,可以看到序列中連續的K個元素,視窗從最左邊出發,每次移動一個單位,對於每次移動,輸出當前視窗中的最大值和最小值 單調遞減佇列:從隊頭到隊尾單調遞減的佇列 入隊操作,先把隊尾元素中小於X的元

POJ 2823 Sliding WindowRMQ壓縮(長度確定)

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

POJ 2823Sliding Window單調佇列

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

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

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

單調佇列 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 (單調佇列)

題目連結 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的滑窗從左向右移動  求每次移動後滑窗區間的最小值和最大值  輸出兩行  第一行所有最小值  第二行所有最大值 可以用線段樹來做  但是單調佇列更簡單  單調遞增佇列: 隊尾單調入隊(入隊元素大於隊尾元素時直接入隊  否則隊尾出隊直到隊尾

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

洛谷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 線段樹求解

題目連結:http://poj.org/problem?id=2823 題意:就是求區間最小值。 此題,顯然可以用線段樹過,但是,知道此題是學習滑動視窗時。此題用滑動視窗做的話比較省程式碼,記憶體和時間。但是要是真正到了賽場上,可能真的不太能想起用滑動視窗,而且對單調佇列也

poj 2823 Sliding Window 線段樹

Sliding Window Time Limit: 12000MS Memory Limit: 65536K Total Submissions: 50906 Accepted: 14640 Case Time Limit: 5000MS Des