zoukankan      html  css  js  c++  java
  • poj 2823 单调队列

    //单调队列求滑动窗体的最大值和最小值
    //题意是给一个n个数,在每k个数区间内
    //求最大值和最小值
    
    //单调队列:队列中的元素是单调的。

    //求最小值的时候:进队的时候将队尾部大于当前要进的元素所有出队 //这样。队列的头部就是最小值 //反之,求最大值也是一样 #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> using namespace std; const int maxn = 1e6 + 8; int a[maxn]; int n,k; int vmi[maxn]; int vmx[maxn]; int deq[maxn]; void input(){ for (int i=1;i<=n;i++){ scanf("%d",&a[i]); } } void getmin(){ int head=0,tail=0; int cnt = 0; for (int i=1;i<=n;i++){ while(tail > head && a[deq[tail-1]]>=a[i]) tail--; deq[tail++] = i; if (i-k+1>=0){ vmi[i-k+1] = a[deq[head]]; if (i - k + 1 == deq[head]) // 这里的意思是最小值是第x个区间的最左边的值,这个值不在求的 //下一个区间内 head++; } } } void getmax(){ int head = 0,tail = 0; int cnt = 0; for (int i=1;i<=n;i++){ while (tail > head && a[deq[tail-1]] <= a[i]) tail--; deq[tail++] = i; if (i-k+1>=0){ vmx[i-k+1] = a[deq[head]]; if (i-k+1 == deq[head]){ // 这里的意思是最小值是第x个区间的最左边的值,这个值不在求的 //下一个区间内 head ++ ; } } } } void print(int a[]){ for (int i=1;i<=n-k+1;i++){ printf("%d%c",a[i],(i==n-k+1)?

    ' ':' '); } } void solve(){ getmax(); getmin(); print(vmi); print(vmx); } int main(){ //freopen("1.txt","r",stdin); while(scanf("%d %d",&n,&k)!=EOF){ input(); solve(); } }


  • 相关阅读:
    kolla多节点部署openstack
    归并排序
    gitlab ci/cd
    微信、支付宝个人收款码不能用于经营收款 z
    微信小程序弹出和隐藏遮罩层动画以及五星评分
    centos7 安装 nginx
    netty+websocket模式下token身份验证遇到的问题
    windows 截图 win+shift+s
    linux下 "chmod 777" 中777这个数字是怎么出来的
    nginx四层转发,访问内网mysql数据库
  • 原文地址:https://www.cnblogs.com/wzzkaifa/p/7112295.html
Copyright © 2011-2022 走看看