zoukankan      html  css  js  c++  java
  • 题解【洛谷P1886】滑动窗口 /【模板】单调队列

    题面

    单调队列模板题。

    单调队列可以从队首和队尾出队。

    队列中的元素大小具有一定的顺序。

    具体可参考这一篇题解

    #include <bits/stdc++.h>
    #define itn int
    #define gI gi
    
    using namespace std;
    
    inline int gi()
    {
    	int f = 1, x = 0; char c = getchar();
    	while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
    	while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    	return f * x;
    }
    
    const int maxn = 1000003;
    
    int n, k, a[maxn], q[maxn], p[maxn];
    
    inline void getmin()
    {
    	int head = 1, tail = 0;
    	for (int i = 1; i <= n; i+=1)
    	{
    		while (head <= tail && q[tail] >= a[i]) --tail;
    		q[++tail] = a[i];
    		p[tail] = i;
    		while (p[head] <= i - k) ++head;
    		if (i >= k) printf("%d ", q[head]);
    	}
    	puts("");
    }
    
    inline void getmax()
    {
    	int head = 1, tail = 0;
    	for (int i = 1; i <= n; i+=1)
    	{
    		while (head <= tail && q[tail] <= a[i]) --tail;
    		q[++tail] = a[i];
    		p[tail] = i;
    		while (p[head] <= i - k) ++head;
    		if (i >= k) printf("%d ", q[head]);	
    	}
    	puts("");
    }
    
    int main()
    {
    	//freopen(".in", "r", stdin);
    	//freopen(".out", "w", stdout);
    	n = gi(), k = gi();
    	for (int i = 1; i <= n; i+=1) a[i] = gi();
    	getmin();
    	getmax();
    	return 0;
    }
    
  • 相关阅读:
    Oracle创建序列,删除序列
    java base58
    百度地图 显示,定位,轮廓图
    百度地图 圈出省份轮廓图并高亮
    基于双向链表的增删改查和排序(C++实现)
    统计字母出现次数
    线程安全
    C++面试秘笈笔记
    牛客选择题刷题
    new delete 浅析
  • 原文地址:https://www.cnblogs.com/xsl19/p/12246845.html
Copyright © 2011-2022 走看看