FZU1894

本文介绍了一个队列问题的解决方法,通过使用单调队列来处理入队、出队及查询最大值的操作。该算法使用C语言实现,并通过数组模拟单调队列,确保了队列内元素的有序性。

题意:队列问题,C为入队列,Q为输出队列中RP的最大值,G为删除队列头的元素

思路:看了别人的思路,用数组模拟单调队列

#include <stdio.h>
#include <string.h>
#define MAXN 1000100

typedef struct {
    int value;
    int num;
}QUEUE;

QUEUE queue[MAXN];

int main() {
    int T;
    char name[10];
    char s[20];
    scanf("%d",&T);
    while (T--) {
        scanf("%s", s);
        int front = 1;  //当前离开人的编号
        int cnt = 0;    //面试人数
        int head = 1;   //表示队列第一个
        int rear = 0;   //队列的长度
        int rp;
        while (scanf("%s",s) != EOF) {
            if (!strcmp(s,"END"))
                break;
            if (strcmp(s, "C") == 0) {
                scanf("%s%d", name, &rp);
                while (head <= rear && rp > queue[rear].value)
                    rear--;
                queue[++rear].value = rp;
                queue[rear].num = ++cnt;
            }
            else if (strcmp(s, "G") == 0) {
                while (head <= rear && queue[head].num <= front)  //单调队列元素下标单调递增
                    head++;
                front++;
            }
            else if (strcmp(s, "Q") == 0)
                printf("%d\n",head > rear ? -1 : queue[head].value);
        }
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值