单调队列 fzu1894 志愿者选拔

传送门:点击打开链接

题意:3种操作:队列中加入一个人;队首出队;求队中权值最大是多少

思路:运用单调队列来维护。其实单调队列中其实id也是有序的,所以队列出队只要看单调队列的第一个是否等于队首那个就行了。

#include<map>
#include<set>
#include<cmath>
#include<ctime>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define fuck(x) cout<<"["<<x<<"]"
#define FIN freopen("input.txt","r",stdin)
#define FOUT freopen("output.txt","w+",stdout)
using namespace std;
typedef long long LL;

const int MX = 1e6 + 5;
const int INF = 0x3f3f3f3f;

char w[100];
int A[MX];
int Q[MX], cur, rear;

int main() {
    int T; //FIN;
    scanf("%d", &T);
    while(T--) {
        int n = 0, L = 1;
        cur = rear = 0;
        while(scanf("%s", w), w[0] != 'E') {
            if(w[0] == 'S') continue;
            if(w[0] == 'C') {
                scanf("%s%d", w, &A[++n]);
                while(cur < rear && A[Q[rear - 1]] < A[n]) rear--;
                Q[rear++] = n;
            } else if(w[0] == 'G') {
                if(cur < rear && Q[cur] == L) cur++;
                L++;
            } else {
                if(cur == rear) printf("-1\n");
                else printf("%d\n", A[Q[cur]]);
            }
        }

    }
    return 0;
}


你可能感兴趣的:(单调队列 fzu1894 志愿者选拔)