ACM-單調隊列之Sliding Window——poj2823

來源:互聯網
上載者:User

標籤:acm   單調隊列   sliding window   poj2823   單調隊列詳解   

Sliding Window
Time Limit: 12000MS   Memory Limit: 65536K
Total Submissions: 36326   Accepted: 10762
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 31 3 -1 -3 5 3 6 7

Sample Output

-1 -3 -3 -3 3 33 3 5 5 6 7

題目:http://poj.org/problem?id=2823

之前在討論 hdu魔板 這道題的時候,有人提到了O(1)隊列,於是 度娘搜尋到了這道題。做一下咯~

①什麼是O(1)隊列?
O(1)隊列又稱單調隊列,顧名思義,就是隊列中的元素是單調的。
要麼單調遞增,要麼單調遞減。
那麼,構建單調隊列有什麼用呢?
我們維護一個隊列後,我們尋找當前隊列內元素最大值/最小值  就可以是O(1)的速度了。
或許有人(比如我剛開始就想過)問:用優先隊列,設定一下優先順序不就可以了。
但是,要知道STL的東東,都挺耗時間的,尤其是 優先隊列。
一般用單調隊列做的題,對時間要求比較高,所以優先隊列顯然不能滿足要求。
②單調隊列如何維護
以單調遞增隊列來舉例:

1、如果隊列的長度一定,先判斷隊首元素是否在規定範圍內,如果超範圍則增長隊首。

2、每次加入元素時和隊尾比較,如果當前元素小於隊尾且隊列非空,則減小尾指標,隊尾元素依次出隊,直到滿足隊列的調性為止

要特別注意頭指標和尾指標的應用。

參考資料:http://www.cnblogs.com/neverforget/archive/2011/10/13/ll.html

http://xuyemin520.is-programmer.com/posts/25964

And 這道題就可以解決了。

/******************************************************************************        Author:Tree                  **From :http://blog.csdn.net/lttree    ** Title : Sliding Window              **Source: poj 2823                     ** Hint  : 單調隊列                   ******************************************************************************/#include <stdio.h>#define MAX 1000001int n,k;int pre1,pre2,lst1,lst2,len_max,len_min;    // 兩個隊列的頭指標與尾指標,最大值的下標,最小值的下表int num[MAX],Increase[MAX],Decrease[MAX],Max[MAX],Min[MAX];      // Num存資料,遞增與遞減隊列,最大值,最小值的數組。// 遞增序列的壓入操作void in_max(int i){    while( pre1<=lst1 && num[ Increase[lst1] ]<num[i] )        --lst1;    Increase[++lst1]=i;    // 如果大於等於k個數,就需要向最大值數組賦值    if( i>=k )    {        if(Increase[pre1]<=i-k)            pre1++;        Max[len_max++]=num[ Increase[pre1] ];    }}// 遞減序列的壓入操作void in_min(int i){    while( pre2<=lst2 && num[ Decrease[lst2] ]>num[i] )        --lst2;    Decrease[++lst2]=i;    // 如果大於等於k個數,就需要向最小值數組賦值    if( i>=k )    {        if(Decrease[pre2]<=i-k)            pre2++;        Min[len_min++]=num[ Decrease[pre2] ];    }}int main(){    int i;    while( ~scanf("%d%d",&n,&k) )    {        // 初始化        pre1=pre2=len_max=len_min=0;        lst1=lst2=-1;        // 讀入資料        for(i=1;i<=n;++i)        {            scanf("%d",&num[i]);            in_max(i);            in_min(i);        }        // 輸出資料        for( i=0;i<len_min-1;++i )            printf("%d ",Min[i]);        printf("%d\n",Min[len_min-1]);        for( i=0;i<len_max-1;++i )            printf("%d ",Max[i]);        printf("%d\n",Max[len_max-1]);    }    return 0;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.