【單調(diào)隊列】POJ_2823_Sliding Window

Sliding Window
Time Limit: 12000MS Memory Limit: 65536K
Total Submissions: 55654 Accepted: 16011
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
POJ Monthly--2006.04.28, Ikki

題意:
有一個n個元素的整數(shù)序列,一個能容納k個元素的滑動窗口花椭,窗口從頭滑至尾,求每一個區(qū)間中的最小數(shù)和最大數(shù)答姥。

思路:
單調(diào)隊列。維護兩個單調(diào)隊列垃喊,分別嚴格單調(diào)增称开、嚴格單調(diào)減,若隊頭元素距離當前元素超過k搓侄,則需出隊,該隊頭元素既是區(qū)間最大/最小话速。

#include<cstdio>
#include<deque>
using namespace std;

const int maxn = 1000000;

struct Node {
    int value, pos;
    Node(int v = -1, int p = -1) :value(v), pos(p) {};
};

deque<Node> minque;
deque<Node> maxque;
int maxans[maxn + 10];
int minans[maxn + 10];

int main() {
    int n, m, th = 0;
    int num;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; ++i) {
        scanf("%d", &num);
        if (i >= m) {
            minans[th] = minque.front().value;
            maxans[th++] = maxque.front().value;
            // 除去不屬于同一區(qū)間的元素
            while (!minque.empty() && i - minque.front().pos >= m)
                minque.pop_front();
            while (!maxque.empty() && i - maxque.front().pos >= m)
                maxque.pop_front();
        }
        // 入隊讶踪,維護隊列單調(diào)
        while (!minque.empty() && minque.back().value >= num)
            minque.pop_back();
        minque.push_back(Node(num, i));
        while (!maxque.empty() && maxque.back().value <= num)
            maxque.pop_back();
        maxque.push_back(Node(num, i));
    }
    minans[th] = minque.front().value;
    maxans[th++] = maxque.front().value;
    for (int i = 0; i < th - 1; ++i)
        printf("%d ", minans[i]);
    printf("%d\n", minans[th - 1]);
    for (int i = 0; i < th - 1; ++i)
        printf("%d ", maxans[i]);
    printf("%d\n", maxans[th - 1]);
    return 0;
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市泊交,隨后出現(xiàn)的幾起案子乳讥,更是在濱河造成了極大的恐慌柱查,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,036評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件云石,死亡現(xiàn)場離奇詭異唉工,居然都是意外死亡,警方通過查閱死者的電腦和手機汹忠,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評論 3 395
  • 文/潘曉璐 我一進店門淋硝,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人宽菜,你說我怎么就攤上這事谣膳。” “怎么了赋焕?”我有些...
    開封第一講書人閱讀 164,411評論 0 354
  • 文/不壞的土叔 我叫張陵参歹,是天一觀的道長仰楚。 經(jīng)常有香客問我隆判,道長,這世上最難降的妖魔是什么僧界? 我笑而不...
    開封第一講書人閱讀 58,622評論 1 293
  • 正文 為了忘掉前任侨嘀,我火速辦了婚禮,結(jié)果婚禮上捂襟,老公的妹妹穿的比我還像新娘咬腕。我一直安慰自己,他們只是感情好葬荷,可當我...
    茶點故事閱讀 67,661評論 6 392
  • 文/花漫 我一把揭開白布涨共。 她就那樣靜靜地躺著,像睡著了一般宠漩。 火紅的嫁衣襯著肌膚如雪举反。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,521評論 1 304
  • 那天扒吁,我揣著相機與錄音火鼻,去河邊找鬼。 笑死雕崩,一個胖子當著我的面吹牛魁索,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播盼铁,決...
    沈念sama閱讀 40,288評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼粗蔚,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了饶火?” 一聲冷哼從身側(cè)響起鹏控,我...
    開封第一講書人閱讀 39,200評論 0 276
  • 序言:老撾萬榮一對情侶失蹤冬念,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后牧挣,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體急前,經(jīng)...
    沈念sama閱讀 45,644評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,837評論 3 336
  • 正文 我和宋清朗相戀三年瀑构,在試婚紗的時候發(fā)現(xiàn)自己被綠了裆针。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,953評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡寺晌,死狀恐怖世吨,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情呻征,我是刑警寧澤耘婚,帶...
    沈念sama閱讀 35,673評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站陆赋,受9級特大地震影響沐祷,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜攒岛,卻給世界環(huán)境...
    茶點故事閱讀 41,281評論 3 329
  • 文/蒙蒙 一赖临、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧灾锯,春花似錦兢榨、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至兼雄,卻和暖如春吟逝,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背君旦。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評論 1 269
  • 我被黑心中介騙來泰國打工澎办, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人金砍。 一個月前我還...
    沈念sama閱讀 48,119評論 3 370
  • 正文 我出身青樓局蚀,卻偏偏與公主長得像,于是被迫代替她去往敵國和親恕稠。 傳聞我的和親對象是個殘疾皇子琅绅,可洞房花燭夜當晚...
    茶點故事閱讀 44,901評論 2 355

推薦閱讀更多精彩內(nèi)容