2026.5.16

· · 个人记录

P1886 【模板】单调队列 / 滑动窗口

题目描述

有一个长为 n 的序列 a,以及一个大小为 k 的窗口。现在这个窗口从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最小值和最大值。
例如,对于序列 [1,3,-1,-3,5,3,6,7] 以及 k = 3,有如下过程:

\begin{array}{|c|c|c|}\hline \textsf{窗口位置} & \textsf{最小值} & \textsf{最大值} \\ \hline \verb![1 3 -1] -3 5 3 6 7 ! & -1 & 3 \\ \hline \verb! 1 [3 -1 -3] 5 3 6 7 ! & -3 & 3 \\ \hline \verb! 1 3 [-1 -3 5] 3 6 7 ! & -3 & 5 \\ \hline \verb! 1 3 -1 [-3 5 3] 6 7 ! & -3 & 5 \\ \hline \verb! 1 3 -1 -3 [5 3 6] 7 ! & 3 & 6 \\ \hline \verb! 1 3 -1 -3 5 [3 6 7]! & 3 & 7 \\ \hline \end{array}

输入格式

输入一共有两行,第一行有两个正整数 n,k;\
第二行有 n 个整数,表示序列 a。

输出格式

输出共两行,第一行为每次窗口滑动的最小值;
第二行为每次窗口滑动的最大值。
::::::success[代码] :::::info[思路]{open}

🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔

🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔

🤔🤔 ber,模版题还要看思路吗? 🤔🤔

🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔

🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔🤔

::::warning[数据范围] 对于 50\% 的数据,1 \le n \le 10^5;
对于 100\% 的数据,1\le k \le n \le 10^6,a_i \in [-2^{31},2^{31})。 :::error[🚫禁止不思考就看代码!!!🚫]

#include<bits/stdc++.h>
#include<math.h>
#include<cmath>
using namespace std;
typedef long long ll;
deque<int> q1,q2;
int a[1000005];
int main() {
//  freopen(".in","r",stdin);
//  freopen(".out","w",stdout);
    ios::sync_with_stdio(0);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,k;
    cin>>n>>k;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++){
        while(q2.size()&&a[i]<=a[q2.back()]){
            q2.pop_back();
        }
        q2.push_back(i);
        while(q2.size()&&q2.front()<i-k+1){
            q2.pop_front();
        }
        if(i>=k) cout<<a[q2.front()]<<" ";
    }
    cout<<'\n';
    for(int i=1;i<=n;i++){
        while(q1.size()&&a[i]>=a[q1.back()]){
            q1.pop_back();
        }
        q1.push_back(i);
        while(q1.size()&&q1.front()<i-k+1){
            q1.pop_front();
        }
        if(i>=k) cout<<a[q1.front()]<<" ";
    }
    return 0;
}
/*

*/
//big : localStorage.cp6_player_id='1f88476d-6223-4bd3-b3c9-199ca12066e0'
//small : localStorage.cp6_player_id='60bb16e0-3c7c-4351-af7f-7549a250d4c6'
:::
:::::
::::::

P8661 [蓝桥杯 2018 省 B] 日志统计

题目描述

小明维护着一个程序员论坛。现在他收集了一份“点赞”日志,日志共有 N 行。其中每一行的格式是 ts id,表示在 ts 时刻编号 id 的帖子收到一个“赞”。 现在小明想统计有哪些帖子曾经是“热帖”。如果一个帖子曾在任意一个长度为 D 的时间段内收到不少于 K 个赞,小明就认为这个帖子曾是“热帖”。
具体来说,如果存在某个时刻 T 满足该帖在 [T,T+D) 这段时间内(注意是左闭右开区间)收到不少于 K 个赞,该帖就曾是“热帖”。
给定日志,请你帮助小明统计出所有曾是“热帖”的帖子编号。

输入格式

第一行包含三个整数 N、D 和 K。
以下 N 行每行一条日志,包含两个整数 ts 和 id。

输出格式

按从小到大的顺序输出热帖 id。每个 id 一行。 ::::::success[代码] :::::info[思路]{open}

1.0版本(暴力)

把问题简化:每次循环处理一个帖子.

for(int id=1; id<=100000; id++){

}

2.0版本(暴力的提升版本📈)

还是暴力

对于每一个帖子的处理,可以用单调队列(滑动窗口^*)来实现。

## 3.0版本(暴力2.0的提升版本📈) ###### ~~就是正解。~~ 优化2: 用动态二维数组,分别存储每个编号对应的时间,然后每个一维数组按时间排序。 ``` vector<int> g[N]; for(int 1 = 1;i <= n;i++)cin>>a[i].ts>>a[i].id; sort(a + 1,a + n + 1,cmp); for (int i= 1;i <= n; it+) g[a[i].id].push_back(a[i].ts); ``` 然后再对每个数组进行check即可。 ``` bool check(int id){ deque<int>a;//存点赞的时间 //滑动窗口 for(int i=0;i≤g[id].size();i++){//每一个点赞 //1.清理前面过期的点赞时间 while(q.size() && g[id][i]-q.front()>-d) q.pop_front(); //2.入队 q.push_back(g[id][i]); //3.如果此时大小>=K,符合条件 if (q.size() >= k) return true; } return false; } ``` ::::warning[数据范围] 对于 $50\%$ 的数据,$1 \le K \le N \le 1000$。 对于 $100\%$ 的数据,$1 \le K \le N \le 10^5$,$0 \le id, ts \le 10^5$ :::error[🚫禁止不思考就看代码!!!🚫] ```cpp #include<bits/stdc++.h> #include<math.h> #include<cmath> using namespace std; typedef long long ll; struct node{ int ts,id; }a[100005]; int n,d,k; bool cmp(node a,node b){ if(a.id!=b.id)return a.id<b.id; return a.ts<b.ts; } vector<int> g[100005]; bool check(int id){ deque<int> q; for(int i=0;i<g[id].size();i++){ while(q.size()&&g[id][i]-q.front()>=d) q.pop_front(); q.push_back(g[id][i]); if(q.size()>=k) return true; } return false; } int main() { // freopen(".in","r",stdin); // freopen(".out","w",stdout); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>d>>k; for(int i=1;i<=n;i++) cin>>a[i].ts>>a[i].id; sort(a+1,a+n+1,cmp); for(int i=1;i<=n;i++) g[a[i].id].push_back(a[i].ts); for(int i=0;i<=100000;i++) if(check(i)) cout<<i<<'\n'; return 0; } /* */ //big : localStorage.cp6_player_id='1f88476d-6223-4bd3-b3c9-199ca12066e0' //small : localStorage.cp6_player_id='60bb16e0-3c7c-4351-af7f-7549a250d4c6' ``` ::: :::: ::::: ::::::