zoukankan      html  css  js  c++  java
  • P1886 滑动窗口

    题面

    这题确实是单调队列的模板题,基本没什么太多要处理的东西,就是一个要达到m个数字再输出,那不就是加一个判断的事么。。

    不多bb直接上代码了

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 #include<queue>
     7 using namespace std;
     8 const int N=1000005;
     9 
    10 int n,m,top1,low1,top2,low2;
    11 int ans1[N],ans2[N],q1[N],q2[N],a[N];
    12 
    13 int get(){//快读
    14     char z=getchar();
    15     int y=1,l=0;
    16     while(z>'9'||z<'1'){
    17         if(z=='-'){
    18             y=-1;
    19         }
    20         z=getchar();
    21     }
    22     while(z>='0'&&z<='9'){
    23         l=l*10+z-'0';
    24         z=getchar();
    25     }
    26     return l*y;
    27 }
    28 
    29 int main(){
    30     n=get();m=get();
    31     for(int i=1;i<=n;i++){
    32         scanf("%d",a+i);
    33         while(top1>=low1&&a[q1[top1]]>=a[i])top1--;//维护递增性
    34         q1[++top1]=i;
    35         while(top2>=low2&&a[q2[top2]]<=a[i])top2--;//维护递减性
    36         q2[++top2]=i;
    37         if(i>=m){//当有多于m个数字时输出
    38             while(q1[low1]<i-m+1)low1++;
    39             while(q2[low2]<i-m+1)low2++;
    40             ans1[i-m+1]=a[q1[low1]];//保存答案
    41             ans2[i-m+1]=a[q2[low2]];
    42         }
    43     }
    44     for(int i=1;i<=n-m+1;i++){
    45         printf("%d ",ans1[i]);
    46     }
    47     printf("
    ");
    48     for(int i=1;i<=n-m+1;i++){
    49         printf("%d ",ans2[i]);
    50     }
    51     return 0;
    52 }

    机房写的代码,支持一下啦~~

  • 相关阅读:
    选择排序——Java实现
    冒泡排序——Python实现
    基数排序——Java实现
    Sqoop迁移Hadoop与RDBMS间的数据
    Hive Join
    Hadoop 完全分布式部署(三节点)
    springboot自定义异常处理
    linux下安装redis
    windows 下tomcat重启脚本
    解决rabbin首次请求慢的问题
  • 原文地址:https://www.cnblogs.com/hahaha2124652975/p/11191429.html
Copyright © 2011-2022 走看看