Printer Queue
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 4329 |
|
Accepted: 2269 |
Description
The only printer of the computer Science Students ' Union was experiencing an extremely heavy workload. Sometimes there is a hundred jobs in the printer queue and your may has to wait for hours to get a single page of output.
Because Some jobs is more important than others, the Hacker general have invented and implemented a simple priority system For the print job queue. Now, each job was 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 are 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 I T.
- Otherwise, print job J (and do not put it back in the queue).
In this to, all those importantmuffin recipes, and Hacker general are printing get printed very quickly. Of course, those annoying term papers that others is printing may has to wait for quite some time to get printed, but th At the ' s life.
Your problem with the new policy was that it had become quite tricky to determine when Your print job would actually be comp Leted. You decide to the write a program to the figure of this out. The program would be given the current queue (as a list of priorities) as well as the position of your job in the queue, an D must then calculate how long it'll take until your job was printed, assuming that no additional jobs would be added to T He queue. To Simplifymatters, we assume this 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 to each test case:
- One line with both integers n and m, where N is the number of jobs in the the queue (1≤n≤100) and M are the position of your Job (0≤m≤n? 1). The first position in the queue is number 0, the second are number 1, and so on.
- One linewith n integers in the range 1 to 9, giving the priorities of the jobs in the the queue. The first integer gives 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 would arrive.
Sample Input
31 054 21 2 3 46 01 1 9 1 1 1
Sample Output
125
Source
Northwestern Europe 2006
Inline Fast Const FAST TLE------>0MS
Accode
#include <iostream> #include <cstdio> #include <cstring>using namespace Std;const int Maxn=102;inline int aabs (int a) {return a<0?-a:a;} int A[maxn],st,ed;int Main () {int loop; scanf ("%d", &loop); while (loop--) {int num,pos; scanf ("%d%d", &num,&pos); for (int i=0;i<num;++i) scanf ("%d", &a[i]); A[pos]=-a[pos]; st=0; Ed=num; int cns=0; while ((ed+1)%maxn!=st) {int k=a[st]; St= (st+1)%MAXN; BOOL Flag=true; for (int i=st;i!=ed;i= (i+1)%MAXN) if (Aabs (A[i]) >aabs (k)) {flag=false; A[ed]=k; Ed= (ed+1)%MAXN; Break } if (flag) {cns++; if (k<0) {printf ("%d\n", CNS); Break }}}} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 3125 Printer Queue data structure