UVA - 12100 Printer Queue

题目大意:输入一串数字序列 每个数字代表优先级[1-9],数字越大优先级越高,从头到尾访问序列,当该数字是当前序列的最高优先级时就花费1分钟打印,如果不是最高优先级就将该数字放到序列尾部,输入指定位置,输出该位置数字打印出来时所花时间
解题思路:另外用一个数组保存全部优先级并且排序,然后直接用队列模拟即可

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
struct node {
    int v;
    int jud;
};
queue<node> que;
int ma[1000];
bool cmd(int a, int b) {
    return a > b;
}
int main() {
    int n;
    int m, x;
    cin >> n;
    while(n--) {
        while(!que.empty()) {
            que.pop();
        }
        cin >> m >> x;
        for(int i = 0; i < m; i++) {
            cin >> ma[i];
            node no;
            no.v = ma[i];
            if(i == x)
                no.jud = 1;
            else
                no.jud = 0;
            que.push(no);
        }
        sort(ma, ma+m, cmd);
        int cou = 0, cou2 = 0;
        while(1) {
            node no = que.front();
            if(no.jud == 1 && no.v == ma[cou2]) {
                break;
            }
            que.pop();
            if(no.v == ma[cou2]) {
                cou2++;
                cou++;
            }
            else {
                que.push(no);
            }
        }
        cout << cou+1 << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值