poj3125 Printer Queue

题目:

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

题意:
有一组文件需要打印,每个文件按重要程度设定了优先级(1最低,9最高)。文件按顺序排序,当轮到第一个文件打印时,如果该文件的优先级是该组中最高优先级则打印,耗时一分钟;否则该文件转到队列的尾部。你知道自己的文件在哪儿(位置从0开始计算),问打印完自己的文件需要多少分钟(其他时间忽略,中途没有新的文件加入)。

这就是典型的优先队列的应用。我们知道一般情况,队列都是先进先出,但在某些情况下我们希望按优先级的顺序输出。这种情况下就出现了优先队列。

本题我们可以先将自己文件的位置标记出来,然后每次循环找当前的最大优先级,如果当前文件的优先级是最大的(有多个优先级相同的则按位置顺序处理),则加时并删除此文件(表明已处理)(如果是自己的文件就结束);否则删除此文件并将备份好的该文件的副本插入尾部。

参考代码:

#include 
#include 
#include 
using namespace std;

typedef pair P;

vector

v;//存储每个元素的优先级和这个文件是不是自己的; void init() { v.clear(); } void input(const int n, const int m) { int num; for (int i = 0;i < n;++i) { cin >> num; if (i == m) {//是自己的文件; v.push_back(P(num, 1)); } else { v.push_back(P(num, 0)); } } } int priority_queue_moni(int n, int m) { int ans = 0; while (!v.empty()) { int maxnum = 0;//当前的最大优先级; for (vector

::iterator it = v.begin();it != v.end();++it) { if (it -> first > maxnum) { maxnum = it -> first; } } int i = 0; while (!v.empty()) { P p = v[i]; if (p.first == maxnum) {//当前的最高优先级; ++ans; v.erase(v.begin());//处理完毕; if (p.second == 1) {//是自己的文件; return ans; } break;//当前向量内的优先级发生了改变; } else { v.erase(v.begin());//删除当前的结点; v.push_back(p);//插入到最后; } } } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n, m; cin >> n >> m; init(); input(n, m); int ans = priority_queue_moni(n, m); cout << ans << endl; } return 0; }

你可能感兴趣的:(poj3125 Printer Queue)