【单调队列】滑动窗口

 P1886 滑动窗口

单调队列经典题目直接上代码

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 using namespace std;
 5 const int sz = 1000010;
 6 int n, k;
 7 int q[sz], p[sz], a[sz];
 8 void work_min() {
 9     int head = 1, tail = 0;
10     for(int i = 1; i <= n; i++) {
11         while(head <= tail && q[tail] >= a[i]) tail--;
12         q[++tail] = a[i];
13         p[tail] = i;
14         while(p[head] <= i-k) head++;
15         if(i >= k) printf("%d ", q[head]);
16     }
17     printf("\n");
18 }
19 void work_max() {
20     int head = 1, tail = 0;
21     for(int i = 1; i <= n; i++) {
22         while(head <= tail && a[i] >= q[tail]) tail--;
23         q[++tail] = a[i];
24         p[tail] = i;
25         while(p[head] <= i-k) head++;
26         if(i >= k) printf("%d ", q[head]);
27     }
28     printf("\n");
29 }
30 int main() {
31     scanf("%d%d", &n, &k);
32     for(int i = 1; i <= n; i++)
33         scanf("%d", &a[i]);
34     work_min();
35     work_max();
36     return 0;
37 }

 

posted @ 2018-10-31 16:45  _Hwjia  阅读(138)  评论(0编辑  收藏  举报
                       ::                              ::
                      :;J7, :,                        ::;7:
                      ,ivYi, ,                       ;LLLFS:
                      :iv7Yi                       :7ri;j5PL
                     ,:ivYLvr                    ,ivrrirrY2X,
                     :;r@Wwz.7r:                :ivu@kexianli.
                    :iL7::,:::iiirii:ii;::::,,irvF7rvvLujL7ur
                   ri::,:,::i:iiiiiii:i:irrv177JX7rYXqZEkvv17
                ;i:, , ::::iirrririi:i:::iiir2XXvii;L8OGJr71i
              :,, ,,:   ,::ir@mingyi.irii:i:::j1jri7ZBOS7ivv,
                 ,::,    ::rv77iiiriii:iii:i::,rvLq@huhao.Li
             ,,      ,, ,:ir7ir::,:::i;ir:::i:i::rSGGYri712:
           :::  ,v7r:: ::rrv77:, ,, ,:i7rrii:::::, ir7ri7Lri
          ,     2OBBOi,iiir;r::        ,irriiii::,, ,iv7Luur:
        ,,     i78MBBi,:,:::,:,  :7FSL: ,iriii:::i::,,:rLqXv::
        :      iuMMP: :,:::,:ii;2GY7OBB0viiii:i:iii:i:::iJqL;::
       ,     ::::i   ,,,,, ::LuBBu BBBBBErii:i:i:i:i:i:i:r77ii
      ,       :       , ,,:::rruBZ1MBBqi, :,,,:::,::::::iiriri:
     ,               ,,,,::::i:  @arqiao.       ,:,, ,:::ii;i7:
    :,       rjujLYLi   ,,:::::,:::::::::,,   ,:i,:,,,,,::i:iii
    ::      BBBBBBBBB0,    ,,::: , ,:::::: ,      ,,,, ,,:::::::
    i,  ,  ,8BHWJBBBBBi     ,,:,,     ,,, , ,   , , , :,::ii::i::
    :      iZMOMAQBBM2::::::::::,,,,     ,,,,,,:,,,::::i:irr:i:::,
    i   ,,:;u0MBMOG1L:::i::::::  ,,,::,   ,,, ::::::i:i:iirii:i:i:
    :    ,iuUuuXUkFu7i:iii:i:::, :,:,: ::::::::i:i:::::iirr7iiri::
    :     :rk@Yizero.i:::::, ,:ii:::::::i:::::i::,::::iirrriiiri::,
     :      5BMBBBBBBSr:,::rv2kuii:::iii::,:i:,, , ,,:,:i@petermu.,
          , :r50EZ8MBBBBGOBBBZP7::::i::,:::::,: :,:,::i;rrririiii::
              :jujYY7LS0ujJL7r::,::i::,::::::::::::::iirirrrrrrr:ii:
           ,:  :@kevensun.:,:,,,::::i:i:::::,,::::::iir;ii;7v77;ii;i,
           ,,,     ,,:,::::::i:iiiii:i::::,, ::::iiiir@xingjief.r;7:i,
        , , ,,,:,,::::::::iiiiiiiiii:,:,:::::::::iiir;ri7vL77rrirri::
         :,, , ::::::::i:::i:::i:i::,,,,,:,::i:i:::iir;@Secbone.ii:::