原題:
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, then move 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 important muffin 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 simplify matters, 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 line with 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.
中文大意:
有一台印表機,有很多人要列印東西。列印的任務被放到一個隊列裡面,每個任務都有一個優先順序,其中9的優先順序最高,1的最低。現在給你兩個數,m和n,m表示隊列中有多少個任務,n表示問你隊列當中第n個任務被列印時的時間用了多少,n從0開始算。接下來給你n個數表示n個任務的優先順序。操作方式如下,首先從隊頭裡面取出一個任務,如果有比這個任務優先順序更高的在後面,那麼就把這個任務放到隊尾,否則就把這個任務列印出來。每次列印耗時1分鐘。任務如果有相同優先順序,那麼就按隊列順序列印即可。
#include <bits/stdc++.h>using namespace std;struct node{ int mark,value; bool operator == (const node t) const { return this->mark==t.mark&&this->value==t.value; }};struct cmp{ bool operator () (node a, node b) { return a.value<b.value; }};deque<node> de;priority_queue<node,vector<node>,cmp> pq;int main(){ ios::sync_with_stdio(false); int t,n,m; cin>>t; while(t--) { cin>>n>>m; de.clear(); while(!pq.empty()) pq.pop(); for(int i=0;i<n;i++) { int a; cin>>a; de.push_back(node{i,a}); pq.push(node{i,a}); } node tmp=de[m]; // cout<<de.front().first<<" "<<de.front().second<<endl; // cout<<tmp.first<<" "<<tmp.second<<endl; int ans=1; while(true) { if(de.front()==tmp&&pq.top().value==tmp.value) break; else { if(pq.top().value==de.front().value) { ans++; pq.pop(); de.pop_front(); } else { node t=de.front(); de.pop_front(); de.push_back(t); } } } cout<<ans<<endl; } return 0;}
解決方式:
好久沒做題了,打算把紫書刷完。
使用兩個隊列,一個用雙向隊列直接儲存任務,另外一個隊列用優先隊列,優先順序高的數排在頂端。每次判斷隊頭的任務是否和優先隊列頂端的任務是否相同,如果相同就輸出結果。否則,如果隊頭的優先順序小於堆頂的任務,那麼放到隊列後面,如果優先順序大於等於堆頂的
任務,那麼列印任務即可。