zoukankan      html  css  js  c++  java
  • priority queue优先队列初次使用

    题目,排队打印问题

    Input Format

    One line with a positive integer: the number of test cases (at most 20). 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 ≤ 50) 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 Format

    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.

    Input Sample

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

    Output Sample

    1
    2
    5
    中文说明,第一行为测试样例组数,下面n组中每组2行,1行的地一个为有几个人在排队,第二个为要求的第几个人的时间(从0数起),下面一行每个数字代表一个人而数字代表这个人的任务的重要级别
    题目是这样的,队头的人如果不是队列中级别最高的,则到队尾继续排队(此过程不消耗时间),当队头的人的级别是队列中最高(大于等于)时,则这个人出队,其他人消耗时间+1.
    代码如下
     1     #include <iostream>
     2     #include <queue>
     3     using namespace std;
     4      
     5     int main() {
     6     int z;
     7     cin >> z;
     8     while (z--) {
     9     int n, m, pri;
    10     int count = 0;
    11     priority_queue<int> pri_queue;
    12     queue<int> cou_queue;
    13     queue<int> ini_queue;
    14     cin >> n >> m;
    15     for (int i = 0; i < n; i++) {
    16     cin >> pri;
    17     cou_queue.push(i);
    18     ini_queue.push(pri);
    19     pri_queue.push(pri);
    20     }
    21     while (1) {
    22     while (ini_queue.front() != pri_queue.top()) {
    23     int temp = ini_queue.front();
    24     ini_queue.pop();
    25     ini_queue.push(temp);
    26     temp = cou_queue.front();
    27     cou_queue.pop();
    28     cou_queue.push(temp);
    29     }
    30     count++;
    31     if (cou_queue.front() == m) {
    32     cout << count << endl;
    33     break;
    34     } else {
    35     pri_queue.pop();
    36     cou_queue.pop();
    37     ini_queue.pop();
    38     }
    39     }
    40     }
    41     return 0;
    42     }

    本题学会了优先队列的使用,优先队列出队的都是队列中重要级别最大的那个人,但单独使用优先队列不足以解决问题,要需要使用2个普通队列的结合,一个记录标号,一个记录优先级别.当普通队列的对头和优先队列的队头一样是,则出对,看代码即可理解

  • 相关阅读:
    ObjectARX代码片段二
    外部程序通过COM启动AutoCAD时RPC_E_CALL_REJECTED的问题解决办法
    ObjectARX代码片段一
    Sublime Text 3 修改插件安装位置【sublime text、插件路径、Data】
    Bug的处理
    界面测试的方法要点
    并发用户数、吞吐量、思考时间的计算公式
    常用测试工具下载
    SVN安装配置详解
    Loadrunner录制脚本时选择协议
  • 原文地址:https://www.cnblogs.com/linjj/p/3757565.html
Copyright © 2011-2022 走看看