5.6 周六下午--单调队列1

· · 个人记录

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。

输出格式

输出共两行,第一行为每次窗口滑动的最小值;
第二行为每次窗口滑动的最大值。

输入输出样例 #1

输入 #1

8 3
1 3 -1 -3 5 3 6 7

输出 #1

-1 -3 -3 -3 3 3
3 3 5 5 6 7

说明/提示

【数据范围】
对于 50\% 的数据,1 \le n \le 10^5;
对于 100\% 的数据,1\le k \le n \le 10^6,a_i \in [-2^{31},2^{31})。

思路:单调队列模板用队列进行入队出队模拟即可。

#include<bits/stdc++.h>
using namespace std;
int n,k;
deque<int>q;
int a[1000005];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
    }
    for(int i=1;i<=n;i++)
    {
        while(q.size()&&a[i]<=a[q.back()])q.pop_back();
        q.push_back(i);
        while(q.size()&&q.front()<i-k+1)q.pop_front();
        if(i>=k)cout<<a[q.front()]<<" ";
    }
    while(q.size())q.pop_front();
    cout<<"\n";
    for(int i=1;i<=n;i++)
    {
        while(q.size()&&a[i]>=a[q.back()])q.pop_back();
        q.push_back(i);
        while(q.size()&&q.front()<i-k+1)q.pop_front();
        if(i>=k)cout<<a[q.front()]<<" ";
    }
    return 0;
}

P1440 求m区间内的最小值

题目描述

一个含有 n 项的数列,求出每一项前的 m 个数到它这个区间内的最小值。若前面的数不足 m 项则从第 1 个数开始,若前面没有数则输出 0。

输入格式

第一行两个整数,分别表示 n,m。

第二行,n 个正整数,为所给定的数列 a_i。

输出格式

## 输入输出样例 #1 ### 输入 #1 ``` 6 2 7 8 1 4 3 2 ``` ### 输出 #1 ``` 0 7 7 1 1 3 ``` ## 说明/提示 对于 $100\%$ 的数据,保证 $1\le m\le n\le2\times10^6$,$1\le a_i\le3\times10^7$。 ### 思路:和上题相似,加上判断q.size()即可。 ```cpp #include<bits/stdc++.h> #include<math.h> #include<cmath> using namespace std; typedef long long ll; deque<int> q1; int a[2000005]; int main() { ios::sync_with_stdio(0); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n,m; cin>>n>>m; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<=n;i++){ if(q1.size()) cout<<a[q1.front()]<<'\n'; else cout<<0<<'\n'; while(q1.size()&&a[i]<=a[q1.back()]){ q1.pop_back(); } while(q1.size()&&q1.front()<=i-m){ q1.pop_front(); } q1.push_back(i); } return 0; } ``` # B3667 求区间所有后缀最大值的位置 ## 题目描述 给定一个长度为 $n$ 的数列 $a$,对于其中每个长度为 $k$ 的子区间,请你求出这个这个子区间构成的数列的所有后缀最大值的位置个数。 一个下标 $i$ 是是数列 $b$ 的后缀最大值下标当且仅当:对于所有的 $i < j \leq |b|$,都有 $b_i > b_j$,其中 $|b|$ 表示 $b$ 的元素个数。 ## 输入格式 第一行是两个整数,依次表示操作次数 $n$ 和子区间长度 $k$。 第二行有 $n$ 个整数,第 $i$ 个整数表示 $a_i$。 ## 输出格式 共输出 $n - k + 1$ 行每行一个整数,按左端点从小到大的顺序依次输出每个子区间构成的数列的后缀最大值位置个数。 ## 输入输出样例 #1 ### 输入 #1 ``` 5 3 2 1 3 5 4 ``` ### 输出 #1 ``` 1 1 2 ``` ## 说明/提示 ### 样例 1 解释 第一个子数列:$2, 1, 3$。其中 $3$ 是后缀最大值。 第二个子数列:$1, 3, 5$,其中 $5$ 是后缀最大值。 第三个子数列:$3,5,4$,其中 $5$ 和 $4$ 是后缀最大值。 ### 数据规模与约定 对于全部的测试点,保证 $1 \leq k \leq n \leq 10^6$,$1 \leq x_i \lt 2^{64}$。 ### 思路:和模板相似,加上i<=k的判断即可。 ```cpp #include<bits/stdc++.h> using namespace std; int n,k; deque<int>q; unsigned long long a[1000005]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin>>n>>k; for(int i=1;i<=n;i++) { cin>>a[i]; } for(int i=1;i<=n;i++) { while(q.size()&&a[i]>=a[q.back()])q.pop_back(); q.push_back(i); while(q.size()&&q.front()<i-k+1)q.pop_front(); if(i>=k)cout<<q.size()<<"\n"; } return 0; } ``` # P2629 好消息,坏消息 ## 题目描述 Uim 在公司里面当秘书,现在有 $n$ 条消息要告知老板。每条消息有一个好坏度,这会影响老板的心情。告知完一条消息后,老板的心情等于老板之前的心情加上这条消息的好坏度。最开始老板的心情是 $0$,一旦老板心情到了 $0$ 以下就会勃然大怒,炒了 Uim 的鱿鱼。 Uim 为了不被炒,提前知道了这些消息(已经按时间的发生顺序进行了排列)的好坏度,希望知道如何才能不让老板发怒。 Uim 可以使用一种叫 “倒叙” 的手法,例如有 $n$ 条消息,Uim 可以任取一个整数 $k$($1 \leq k \leq n$),先从 $k$ 事件通报到 $n$ 事件,再从 $1$ 事件通报到 $k-1$ 事件。特别的,当 $k=1$ 时按照原顺序通报。 他希望知道,有多少个这样的 $k$ 可以让老板不发怒。 ## 输入格式 第一行一个整数 $n$($1 \le n \le10^6$),表示有 $n$ 个消息。 第二行 $n$ 个整数,按时间顺序给出第 $i$ 条消息的好坏度 $A_i$($-10^3\le A_i \le 10^3$)。 ## 输出格式 一行一个整数,表示可行的方案个数。 ## 输入输出样例 #1 ### 输入 #1 ``` 4 -3 5 1 2 ``` ### 输出 #1 ``` 2 ``` ## 说明/提示 **【样例解释】** 通报事件的可行顺序(用编号表示)为 $2\rightarrow3\rightarrow4\rightarrow1$ 或 $3\rightarrow4\rightarrow1\rightarrow2$(分别对应 $k=2$ 和 $k=3$) 通报事件的可行顺序(用好坏度表示)为 $5\rightarrow1\rightarrow2\rightarrow(-3)$ 或 $1\rightarrow2\rightarrow(-3)\rightarrow5

【数据范围】

对于 25\% 的数据,n\le10^3;
对于 75\% 的数据,n\le10^4;
对于 100\% 的数据,1 \le n\le 10^6。

思路:这题要用前缀和+单调队列,判断s[r]<=s[q.back()]进行出队,最终输出k.

#include<bits/stdc++.h>
using namespace std;
#define ll  long long
ll n,k;
ll a[10000005];
ll s[10000005];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>n;
    for(ll i=1;i<=n;i++){
        cin>>a[i];
        a[i+n]=a[i];
    }
    for(int i=1;i<=2*n;i++){
        s[i]=a[i]+s[i-1];
    }
    deque<int>q;
    for(int r=1;r<=2*n-1;r++){
        int l=r-n+1;
        while(q.size() && s[r]<=s[q.back()]){
            q.pop_back();
        }
        q.push_back(r);
        while(q.size() && q.front()<l){
            q.pop_front();
        }
        if(r>=n&&s[q.front()]-s[l-1]>=0){
            k++;
        }
    }
    cout<<k;

    return 0;
}

作业:

p2032

#include<bits/stdc++.h>
using namespace std;
deque<int>dq;
int n,k,a[2000001];
int main()
{
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        while(!dq.empty()&&a[i]>a[dq.back()])
        {
            dq.pop_back();
        }
        if(!dq.empty()&&i-dq.front()>=k)
        {
            dq.pop_front(); 
        }
        dq.push_back(i);
        if(i>=k)cout<<a[dq.front()]<<endl;
    }
    return 0;
}

p14735

#include<bits/stdc++.h>
using namespace std;
int n,k,ans=INT_MAX,a[10005],t[10005],tt[10005];
int main()
{
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>n>>k;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        tt[a[i]]++;
    }
    int cnt=1,cnt2=k;
    t[a[1]]=1;
    tt[a[1]]--;
    if(tt[a[1]]==0)cnt2--;
    for(int l=1,r=1;l<= n&&r<=n;l++)
    {
        while(cnt<k&&r<n)        
        {
            r++;
            t[a[r]]++;
            tt[a[r]]--;
            if(t[a[r]]==1)cnt++;
            if(tt[a[r]]==0)cnt2--;
        }
        if(cnt==k&&cnt2==k)ans=min(ans,r-l+1);
        t[a[l]]--;
        tt[a[l]]++;
        if(t[a[l]]==0)cnt--;
        if(tt[a[l]]==1)cnt2++;
    }
    if(ans==INT_MAX)cout<<0<<"\n";
    else cout<<ans<<"\n";
    return 0;
}

B3928

#include<bits/stdc++.h>
using namespace std;
const int N=5e4+1;
int n,a[N],b[N],ans;
int main(){
    ios::sync_with_stdio(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    for(int i=1;i<=n;i++){
        cin>>b[i];
    }
    sort(a+1,a+1+n);
    sort(b+1,b+1+n);
    for(int i=1,j=1;i<=n;i++){
        if(a[i]>=b[j]){
            ++j,++ans;
        }
    }
    cout<<ans;
    return 0;
}