12100:Printer Queue

本文介绍了一个使用优先级队列解决打印任务顺序问题的C++实现方案。通过结合普通队列与优先级队列的数据结构,实现了对一系列打印任务按其优先级进行正确排序的功能。该方案适用于需要按特定顺序处理大量任务的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Printer Queue

priority_queue + queue

#include<cstdio>
#include<queue>
using namespace std;
const int maxn = 500000;
int t,n,pos;
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d %d",&n,&pos);
        queue<int>q;
        priority_queue<int>pq;
        int t;
        for(int i = 0;i < n;i++){
            scanf("%d",&t);
            q.push(t);
            pq.push(t);
        }
        int cnt = 0;
        while(pos >= 0){
            if(q.front() == pq.top()){
                q.pop();pq.pop();
                cnt++;
                pos--;
            }else{
                q.push(q.front());
                q.pop();
                if(!pos) pos = q.size() - 1;
                else pos--;
            }
        }
        printf("%d\n",cnt);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值