zoukankan      html  css  js  c++  java
  • 洛谷 P2032 扫描

    洛谷 P2032 扫描

    思路

    单调队列例题,有多倍经验哦~~

    多倍经验传送门:

    P1886 滑动窗口

    P1440 求m区间内的最小值

    代码

    //知识点:单调队列 
    /*
    By:Loceaner
    */
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    using namespace std;
    
    inline int read() {
    	char c = getchar();
    	int x = 0, f = 1;
    	for( ; !isdigit(c); c = getchar()) if(c == '-') f = -1;
    	for( ; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ 48);
    	return x * f;
    }
    
    const int N = 2e6 + 11;
    
    int n, k, q[N], a[N];
    
    void getmax() {
    	int head = 0, tail = 0;
    	for(int i = 1; i < k; i++) {
    		while(head <= tail && a[q[tail]] <= a[i]) tail--;
    		q[++tail] = i;
    	}
    	for(int i = k; i <= n; i++) {
    		while(head <= tail && a[q[tail]] <= a[i]) tail--;
    		q[++tail] = i;
    		while(q[head] <= i - k) head++;
    		cout << a[q[head]] << '
    ';
    	}
    }
    
    int main() {
     	n = read(), k = read();
     	for(int i = 1; i <= n; i++) a[i] = read();
     	getmax();
    	return 0;
    }
    
  • 相关阅读:
    16
    15
    14
    13
    12
    11
    10
    python包管理器修改镜像地址
    Linux环境下安装hadoop分布式集群+问题总结
    解剖css中的clear属性
  • 原文地址:https://www.cnblogs.com/loceaner/p/11646858.html
Copyright © 2011-2022 走看看