fzu1894 自愿者选拔

题意:

中文题目就不多说了。

思路:

单调队列的使用,因为问的是当前排队中的最高rp,所以用个维护最大值的单调队列就好了,出队和询问的时候判断下就好了。
const int maxn = 1e6 + 10;
struct node {
    char *name;
    int rp;
    int pos;
    node() {}
    node(char c[], int rp,int pos) {
        this->name = c;
        this->rp = rp;
        this->pos = pos;
    } 
}p[maxn];
int main(int argc, const char * argv[])
{   
    // freopen("in.txt","r",stdin);
    // freopen("out.txt","w",stdout);
    int t;
    cin >> t;
    char name[10];
    int rp;
    while(t--) {
        char op[10];
        int head = 0, tail = 0;
        int out = 0;
        int pos = 0;
        while(scanf("%s", op) != EOF) {
            if (strcmp(op, "START") == 0) continue;
            if (op[0] == 'C') {
                scanf("%s %d", name, &rp);
                while(head < tail && p[tail-1].rp < rp) tail--;
                p[tail++] = node(name, rp, ++pos);
            }else if (op[0] == 'G') {
                out++;
                if (p[head].pos == out) head++;
            }else if (op[0] == 'Q'){
                if (head == tail) printf("-1\n");
                else printf("%d\n", p[head].rp);
            }else if (op[0] == 'E') break;
        }
    }
    return 0;
}

你可能感兴趣的:(单调队列)