Sicily 1443.Printer Queue

题目描述:
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.

输入格式:
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.

输出格式:
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.


概要:
题意大致是首先输入n代表n个任务,m代表“我“的任务现在所处的位置,然后输入一系列数字代表任务优先级和目前位置,数字越大优先级越高,位置从0开始计算。当位置为0的任务优先级不是现有任务中最高的时候,就会被移到队列的末尾,若为最高优先级则直接弹出队列,时间增加一分钟。直到自己的任务被弹出,并将时间打印出来。

思路:
采用queue记录队列中的任务,并用数组存储队列中含有的任务的优先级,并按照从大到小的顺序排列。当queue.front()=priority[0]时,说明任务可以被弹出,时间+1。m记录“我“的任务在队列的位置,当m=0 且 queue.front()=priority[0]时,说明“我“的任务可以被弹出,时间+1 并输出。

代码实现:

#include 
#include 
using namespace std;
void qs(int a[],int l,int r){           //quick sort
    int t,i = l,j = r,m = a[(l+r) / 2];
    while(i <= j) {
        while(a[i] > m && i < r) ++ i;
        while(a[j] < m && j > l) -- j;
        if(i <= j)
        {
            t = a[i];
            a[i] = a[j];
            a[j] = t;
            ++ i;   
            -- j;
        }
    }
    if(i <= r) qs(a,i,r);
    if(j >= l) qs(a,l,j);
}
int calculate(int p[],int n,int m,queue<int> print)
{
    qs(p,0,n-1);
    int result=0;

    while(!print.empty())
    {
        if(m == 0 && print.front() == p[0]) return result+1;
        else if(m == 0 ) {print.push(print.front()); print.pop(); m=print.size()-1;}
        else if(print.front() == p[0]) 
        {
            print.pop();
            m --;
            result ++;
            for(int i=0;i1;i++) p[i]=p[i+1];
        }
        else 
        {
            print.push(print.front());
            print.pop();
            m --;
        }
    }
}
int main()
{
    int cases;
    cin >> cases;

    while(cases)
    {
        int n,m;
        cin >> n >> m;
        int priority[100];
        queue<int> print;

        for(int i=0;icin >> priority[i]; print.push(priority[i]);}

        int t=0;
        t=calculate(priority,n,m,print);

        cout << t << endl;
        cases --;
    }
}

思路比较简单,实现起来并不是特别困难。如果有更好的方法实现,欢迎大家提出批评和指导。


第一次写Blog,也是希望自己能多花点时间在学习上面,记录下来也算一种激励吧。

你可能感兴趣的:(Sicily 1443.Printer Queue)