UVALive3638 UVA12100 POJ3125 HDU1972 Printer Queue【队列+模拟】

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5332   Accepted: 2739

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

Source



Regionals 2006 >> Europe - Northwestern


问题链接UVALive3638 UVA12100 POJ3125 HDU1972 Printer Queue

题意简述:一个打印队列中的任务是无序的,每个任务有一个优先级,打印机会从第一个任务开始执行,如果当前任务的优先级是队列中最大的,那么就打印,否则就将该任务放置到队末。每次打印消耗一分钟的时间,求该序列中的指定任务m分别在什么时候被打印完成。

问题分析:按照优先级从高到低顺序执行,先执行所有比作业m优先级高的作业。

程序说明:(略)


AC的C++语言程序如下:

/* UVALive3638 UVA12100 POJ3125 HDU1972 Printer Queue */

#include <iostream>
#include <queue>
#include <algorithm>

using namespace std;

const int N = 100;
int jobs[N];

int main()
{
    int t, n, m;
    queue<int> q;

    cin >> t;
    while(t--) {
        cin >> n >> m;

        for(int i=0; i<n; i++) {
            cin >> jobs[i];
            q.push(jobs[i]);
        }

        sort(jobs, jobs + n);

        int first = m, last = n - 1, ans = 1;
        for(;;) {
            if(first < 0)
                first = q.size() - 1;
            if(q.front() == jobs[last]) {
                if(first == 0)
                    break;
                q.pop();
                ans++;
                first--;
                last--;
            } else {
                first--;
                q.push(q.front());
                q.pop();
            }
        }

        cout << ans << endl;

        while(!q.empty())
            q.pop();
    }

    return 0;
}



原文地址:https://www.cnblogs.com/tigerisland/p/7563653.html