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 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
彼此英文都不好,又何苦互相为难呢?不过这也提醒我该多学英语!!题目的大意是要打印一堆job,每个job有其对应的优先级,当排在最前面的job的优先级不是最高的时候,不打印,把job移动到最后;排在最前面的是最高优先级的时候,打印。多个测试用例,每个用例输入包含n m,代表job个数,和你要打印的job在原始队列中的位置,接下来是一串数字,代表对应位置的job的优先级。你需要figure out指导你的job打印完成一共花了多长时间。每一次打印花费1时间,移动不耗时。
思路:最简单暴力的办法当然就是老老实实按照题意模拟啦。你可以开一个类,类的成员是priority(优先级),和ismyjob(是否是我的job)
然后另外开一个int数组记录比myjob优先级高的job的优先级,并且对着这个数组进行降序排序,这里要用到algorithm的sort,并且由于sort默认是升序,还要加一个compare函数来改为降序;
接下来就是模拟啦,用一个存储job对象的队列模拟吧。先把高优先级的全部弹出(这个不用说吧),接下来就是myjob的优先级最高。接下来如果队首的元素优先级低于myjob,当然是不打印,移动到最后,如果和myjob同样优先级,但是ismyjob为false,那打印并且时间+1,直到打印myjob为止。不能再说了= =。
代码+注释:这次也是摸索着打,优化的余地很大,不忍卒读
#include <iostream> #include <queue> #include <string> #include <algorithm> using namespace std; class job{ public: int priority; bool ismyjob; job(){ priority = -1; ismyjob = false; } }; bool compare(int a, int b){ return a > b; } int main(){ int i, txcase, numofjob, pos, myjobpriority; cin >> txcase; while(txcase--){ queue<job> imitate; job toprint[100]; int counter = 0; bool printmyjob = false; int currenthighpy[100]; int waitingtime = 0; cin >> numofjob >> pos; for(i = 0;i < numofjob;i++){ cin >> toprint[i].priority; } toprint[pos].ismyjob = true; myjobpriority = toprint[pos].priority; for(i = 0;i < numofjob;i++){ imitate.push(toprint[i]);//初始化队列 if(toprint[i].priority > myjobpriority){ currenthighpy[counter] = toprint[i].priority; counter++;//记录高于myjob的优先级的job数目 } }//循环之后,currenthighpy数组内就存储着比myjobpriority高优先级的全部job sort(currenthighpy,currenthighpy+counter,compare);//添加一个compare来实现降序排序 while(!printmyjob){//当myjob还未被打印 i = 0; while(counter > 0){ if((imitate.front()).priority < currenthighpy[i]){ imitate.push(imitate.front()); imitate.pop(); } else if((imitate.front()).priority == currenthighpy[i]){ imitate.pop(); waitingtime++; i++; counter--; } }//走完这个循环之后高优先级的全部已经出队列 while(!printmyjob){ if((imitate.front()).priority < myjobpriority){ imitate.push(imitate.front()); imitate.pop(); } else if((imitate.front()).priority == myjobpriority && (imitate.front()).ismyjob != true){ imitate.pop(); waitingtime++; } else if((imitate.front()).ismyjob){ printmyjob = true; waitingtime++; } } } while(!imitate.empty()) imitate.pop(); cout << waitingtime << endl; } return 0; }