zoukankan      html  css  js  c++  java
  • Sicily 1443. Printer Queue 解题报告

    题目:

    Constraints

    Time Limit: 1 secs, Memory Limit: 32 MB

    Description

    The only printer in the computer science students' union is experiencing an extremely heavy workload. Sometimes there are a hundred jobs in the printer queue and you may have to wait for hours to get a single page of output.

    Because some jobs are more important than others, the Hacker General has invented and implemented a simple priority system for the print job queue. Now, each job is assigned a priority between 1 and 9 (with 9 being the highest priority,
    and 1 being the lowest), and the printer operates as follows.

    • The first job J in queue is taken from the queue.
    • If there is some job in the queue with a higher priority than job J, thenmove J to the end of the queue without printing it.
    • Otherwise, print job J (and do not put it back in the queue).

    In this way, all those importantmuffin recipes that the Hacker General is printing get printed very quickly. Of course, those annoying term papers that others are printing may have to wait for quite some time to get printed, but that's life.

    Your problem with the new policy is that it has become quite tricky to determine when your print job will actually be completed. You decide to write a program to figure this out. The program will be given the current queue (as a list of priorities) as well as the position of your job in the queue, and must then calculate how long it will take until your job is printed, assuming that no additional jobs will be added to the queue. To simplifymatters, we assume that printing a job always takes exactly one minute, and that adding and removing jobs from the queue is instantaneous.

    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

    思路:

    按照题目要求如果队列第一个任务不是最高优先级的则将其移到队列尾部,理所当然用deque作为数据存储结构。

    由于题目给定一个位置的任务,而随着打印的进行各个元素的位置不断发生变化,所以要记录每个元素的初始位置。

    这里用PrintingJob类模拟要打印的任务,其中包括该任务的初始位置和优先级2个私有成员。按照题目要求,只有当队列第一个元素是目标元素而且没有别的元素优先级比它高时才能成功将其打印,因此main函数里面用了一个while循环模拟队列的不断移动,如果队列首个元素优先级最高时即可将其pop掉。

     1 #include<iostream>
     2 #include<deque>
     3 #include<algorithm>
     4 using namespace std;
     5 
     6 class PrintingJob{
     7 private:
     8     int position;
     9     int priority;
    10 public:
    11     void get_data(int pos,int pri){
    12         position=pos;
    13         priority=pri;
    14     }
    15     int getPosition(){
    16         return position;
    17     }
    18     int getPriority(){
    19         return priority;
    20     }
    21     bool is_highest_priority(deque<PrintingJob>&jobs){//判断一个任务是否是队列中优先级最高的(没有任务优先级高于它)
    22         deque<PrintingJob>::iterator it=jobs.begin();
    23         while(it!=jobs.end()){
    24             if(it->priority>priority)
    25                 return false;
    26             it++;
    27         }
    28         return true;
    29     }
    30 };
    31 
    32 int main(){
    33     int t;
    34     cin>>t;
    35     while(t--){
    36         int num_of_jobs,position_of_target;
    37         int time=1;//最少需要打印目标任务的时间
    38         deque<PrintingJob>jobs;
    39         cin>>num_of_jobs>>position_of_target;
    40         for(int i=0;i<num_of_jobs;i++){
    41             //将任务压入双端队列中
    42             int priority;
    43             PrintingJob ajob;
    44             cin>>priority;
    45             ajob.get_data(i,priority);
    46             jobs.push_back(ajob);
    47         }
    48         while(!(jobs.begin()->getPosition()==position_of_target
    49                 &&jobs.begin()->is_highest_priority(jobs))){
    50             //在发现队列首元素是目标任务且没有其他任务优先级比它高时可以完成打印跳出循环
    51             if(jobs.begin()->is_highest_priority(jobs)){//在队列首的任务优先级最高可以打印出来
    52                 jobs.pop_front();
    53                 time++;
    54             }
    55             else{
    56                 //将队列首元素放到队列末尾
    57                 PrintingJob ajob;
    58                 ajob.get_data(jobs.begin()->getPosition(),jobs.begin()->getPriority());
    59                 jobs.pop_front();
    60                 jobs.push_back(ajob);
    61             }
    62         }
    63         cout<<time<<endl;
    64     }
    65     return 0;
    66 }
  • 相关阅读:
    WinForm 清空界面控件值的小技巧
    MVC 图片上传总是request.files.count() 等于0
    LinqToSQL 左连接右连接内链接
    C# utc datetime 互相转化
    mvc javascript form.submit()
    Java API中文版下载
    jQuery的链式操作
    【转】Eclipse/MyEclipse中使用复制粘贴功能卡的解决办法
    servlet什么时候被实例化?
    Jquery总结 $("h3 a", patch);
  • 原文地址:https://www.cnblogs.com/jolin123/p/3369841.html
Copyright © 2011-2022 走看看