1443. Printer Queue

本文介绍了一个使用C++实现的打印队列模拟程序。该程序通过输入一系列工作队列的任务优先级,模拟了打印机的工作流程,计算并输出特定任务完成所需的分钟数。通过运用vector容器和自定义的数据结构node来存储任务的索引和优先级,实现了队列的高效操作。

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

注:c++实验课堂题目

Input

One line with a positive integer: the number of test cases (at most 100). Then for each test case:

One line with two integers n and m, where n is the number of jobs in the queue (1 ≤ n ≤ 100) and m is the position of your job (0 ≤ m ≤ n −1). The first position in the queue is number 0, the second is number 1, and so on.
One linewith n integers in the range 1 to 9, giving the priorities of the jobs in the queue. The first integer gives the priority of the first job, the second integer the priority of the second job, and so on.

Output

For each test case, print one line with a single integer; the number of minutes until your job is completely printed, assuming that no additional print jobs will arrive.

Sample Input

3
1 0
5
4 2
1 2 3 4
6 0
1 1 9 1 1 1

Sample Output

1
2
5

// 用vector实现
// 注意善用node使得数组能够存储多个信息,这些信息其实包含在node中
#include<iostream>
#include<vector>
using namespace std;

struct node {
  int index;
  int priority;
  node(int i = 0, int p = 0):index(i), priority(p) {};
};

int main() {
  int t;
  cin >> t;
  while (t--) {
    vector<node> que;
    int alljob, myjob;
    cin >> alljob >> myjob;
    int temp;
    for (int i = 0; i < alljob; ++i) {
      cin >> temp;
      que.push_back(node(i, temp));
    }
    int result = 0;
    while (!que.empty()) {
      node top = que.front();
      bool flag = false;
      for (int i = 1; i < que.size(); ++i) {
        if (que[i].priority > que[0].priority) {
          que.push_back(top);
          flag = true;
          break;
        }
      }
      que.erase(que.begin());
      if (flag == false) result++;
      if (flag == false && top.index == myjob) break;
    }
    cout << result << endl;
  }
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值