zoukankan      html  css  js  c++  java
  • poj 2823

    才发现我的第一道BZ是单调队列。。

    发现云神的资料里面有这个的介绍就来水水。。

    单调队列由于要去掉队尾就还是用数组好点,而且写起来比STL要好看点

     1 //#include<bits/stdc++.h>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<iostream>
     6 #include<queue>
     7 #define inc(i,l,r) for(int i=l;i<=r;i++)
     8 #define dec(i,l,r) for(int i=l;i>=r;i--)
     9 #define link(x) for(edge *j=h[x];j;j=j->next)
    10 #define mem(a) memset(a,0,sizeof(a))
    11 #define inf 1e9
    12 #define ll long long
    13 #define succ(x) (1<<x)
    14 #define NM 1000000+5
    15 using namespace std;
    16 int read(){
    17     int x=0,f=1;char ch=getchar();
    18     while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    19     while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    20     return x*f;
    21 }
    22 int n,m,q[NM],a[NM],qh,qt,k;
    23 int main(){
    24 //    freopen("data.in","r",stdin);
    25     n=read();k=read();
    26     inc(i,1,n)a[i]=read();qh=1;
    27     inc(i,1,k-1){
    28         while(qh<=qt&&a[q[qt]]>=a[i])qt--;
    29         q[++qt]=i;
    30     }
    31     inc(i,k,n){
    32         while(qh<=qt&&a[q[qt]]>=a[i])qt--;
    33         while(qh<=qt&&q[qh]<i-k+1)qh++;
    34         q[++qt]=i;printf("%d ",a[q[qh]]);
    35     }
    36     printf("
    ");mem(q);qh=1;
    37     inc(i,1,k-1){
    38         while(qh<=qt&&a[q[qt]]<=a[i])qt--;
    39         q[++qt]=i;
    40     }
    41     inc(i,k,n){
    42         while(qh<=qt&&a[q[qt]]<=a[i])qt--;
    43         while(qh<=qt&&q[qh]<i-k+1)qh++;
    44         q[++qt]=i;printf("%d ",a[q[qh]]);
    45     }
    46     printf("
    ");
    47     return 0;
    48 }
    View Code
  • 相关阅读:
    数组常用方法
    Chrome调式技巧
    Object.create()和new object()和{}的区别
    ES6基础知识
    sass基础
    webpack中package.json相关参数
    webpack.config.js====图片处理
    java发送邮件功能[转]
    mybatis sql使用经验总结
    JSON操作
  • 原文地址:https://www.cnblogs.com/onlyRP/p/5131413.html
Copyright © 2011-2022 走看看