Printer Queue

也可以查看这里
题目链接

题目大意:给出长度为n的一个队列,你所在的位置是m,0 <= m < n,每个任务有一个优先级,如果队列中没有优先级比当前队头的任务的优先级高,那么就直接打印这个任务,否则,就将这个任务添加到队列末尾

思路:直接用queue模拟即可

坑点:如果一个任务不打印的话是不消耗时间的

代码:

#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 110;
typedef pair < int , int > PII;

int cnt[20];//cnt数组用于记录当前队列中每个优先级的任务有多少个

//判断函数,判断当前这个任务是否可以打印
bool judge(int x)
{
    for(int i = 9 ; i > x ; -- i)
        if(cnt[i]) return false;
    return true;
}

int main(void)
{
    int T;
    cin >> T;
    while(T --)
    {
        queue <PII> q;
        
        int n , m;
        scanf("%d %d",&n , &m);
        
        memset(cnt , 0 , sizeof cnt);
        for(int i = 0 ; i < n ; ++ i)
        {
            int a;
            scanf("%d",&a);
            q.push({a , i});
            cnt[a] ++;
        }
        
        int res = 0;
        
        while(true)
        {
            int a = q.front().first;
            int b = q.front().second;
            if(judge(a))
            {
                cnt[a] --;
                q.pop();
                
                res += 1;
                
                if(b == m) break;
            }
            
            else
            {
                PII a = q.front();
                q.pop();
                q.push(a);
            }
        }
        printf("%d\n",res);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值