Basic queue operations in Sicily 1443

Source: Internet
Author: User
1443. printer queue Constraints

Time Limit: 1 secs, memory limit: 32 MB

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 defined Ted 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
31 054 21 2 3 46 01 1 9 1 1 1
Sample output
125

# Include <iostream> # include <vector> # include <algorithm> using namespace STD; // structure chart paper is used to store the priority and position of each paper of the original input, without sorting, actual Operation typedef struct paper {int priority; int position;} paper; bool CMP (int A, int B) {return A> B;} int main () {INT cases; cin> cases; while (cases --> 0) {int number; int target_p; int minute = 0; vector <int> papers; // sort the generated vector <Paper> o_papers in the descending order of priority queue by priority; // the priority of each paper that stores the original input Level and position, not sorted. The actual simulated printer operation int paper; CIN> number> target_p; For (INT I = 0; I <number; I ++) {CIN> paper; papers. push_back (paper); paper temp; temp. priority = paper; temp. position = I; o_papers.push_back (temp);} Sort (papers. begin (), papers. end (), CMP); For (INT I = 0; I <papers. size (); I ++) {// print the while (o_papers [0]. priority! = Papers [I]) {// find the highest priority paper o_papers.push_back (o_papers [0]); o_papers.erase (o_papers.begin ());} // If (o_papers [0]. position = target_p) {// The target paper exit loop minute ++; break;} else {// not the target paper, which is deleted from the o_papers queue, that is, the print operation minute ++; o_papers.erase (o_papers.begin () ;}} cout <minute <Endl;} return 0 ;}

  

Basic queue operations in Sicily 1443

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.