UVA 12100 Printer Queue

Source: Internet
Author: User

Original question:
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 syste M 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, then move J to the end of the queue without Printin G it.
Otherwise, print job J (and do not put it back in the queue).
In this to, all those important muffin 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 simplify matters, 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 a integers n and m, where N is the number of jobs in the the queue (1≤n≤100) and M are the position of yo ur job (0≤m≤n−1). The first position in the queue is number 0, the second are number 1, and so on.
one line with n integers in the range 1 to 9, giving the priorities of the jobs in the the queue. The first integer gives the priority of the first job, the second integer the first 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.

The main idea:
There is a printer, there are a lot of people to print things. The printed task is placed in a queue, with each task having a priority, with 9 having the highest priority and a minimum of 1. Now give you two numbers, M and n,m indicate how many tasks are in the queue, n indicates how much time is spent in the queue when the nth task is printed, and N starts at 0. Next you'll be given the n number to indicate the priority of the N task. The operation is as follows, first remove a task from the team head, if there is a higher priority than the task in the back, then put the task at the end of the team, or the task to print out. It takes 1 minutes to print each time. If the task has the same priority, it is printed in the order of the queue.

#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;
 }

Workaround:
Long time no problem, plan to finish the purple book. The
uses two queues, one that stores tasks directly with two-way queues, the other queue with a priority queue, and a high priority number at the top. Each time you determine whether the task of the team head is the same as the task at the top of the priority queue, the output is the same. Otherwise, if the priority of the team header is less than the task at the top of the heap, then the task can be printed after the queue, if the priority is greater than the
task at the top of the heap.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.