uva 12100 Printer Queue 习题5-7 打印队列 (STL deque练习)

题目:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3252


题意:输入一个队列,每个元素都有优先级,如果队首是优先级最高的,则符合要求,出队;否则移到队尾。问初始队列中第i个元素经过多久才出队的。


说明:queue中似乎没有遍历元素的方法,用deque可以遍历队列,同时实现queue的功能。


#include<stdio.h>
#include<queue>
#include<iostream>
using namespace std;
struct printer{
    int dat;
    int num;
}a;
int main(){
    int T,N,M;
    cin >> T;
    while(T--){
        deque<printer> p;
        cin >>N >>M;
        int tot=0;//tot代表总数,也就是时间

        for(int i=0;i<N;i++)
        {
            cin>> a.dat;
            a.num=i;
            p.push_back(a);
        }

        while(!p.empty()){
            int maxn=-1;
            for(int i=0;i<p.size();i++) //求当前队列中最高的优先级
                if(maxn<p[i].dat) maxn=p[i].dat;

            while(p[0].dat!=maxn){//如果当前队首优先级不是最高,移到队尾
                p.push_back(p[0]);
                p.pop_front();
            }

            tot++;
            if(p[0].num==M) break;
            p.pop_front();//队首出列
        }
        cout << tot<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值