[模板]队列的四种方法

· · 算法·理论

[模板]队列四种方法

单调队列

#include<bits/stdc++.h>
using namespace std;
int q[10010];
int front = 1,rear = 0;
int n;
int main(){
    scanf("%d",&n);
    for(int i = 1;i<=n;i++){
        int o,x;
        scanf("%d",&o);
        if(o==1){
            scanf("%d",&x);
            q[++rear] = x;
        }
        if(o==2){
            if(front>rear){
                cout<<"ERR_CANNOT_POP"<<endl;
            }
            else{
                front++;
            }
        }
        if(o==3){
            if(front>rear){
                cout<<"ERR_CANNOT_QUERY"<<endl;
            }
            else{
                cout<<q[front]<<endl;
            }           
        }
        if(o==4){
            cout<<rear-front+1<<endl;
        }
    }
    return 0;
}