/*
直接按照题意一步步操作即可
*/
/*
Run Time: 0secs
Run Memory: 312KB
*/
#include <iostream>
#include <memory.h> //memset初始化
#include <queue>
using namespace std;
typedef struct Job{
int key;
int flag;
};
int n, m;
queue<Job> jobs; //存储所有job的队列
int check[10]; //存储各个优先级各自的job数量
int times;
//获得优先级比当前大的未处理工作数,只有当其为0时才打印
int getLargerNum(int x){
int buf = 0;
for(int i=x+1; i<=9; i++)
buf += check[i];
return buf;
}
void comput(){
while(m != -1){
Job buf = jobs.front();
int num = getLargerNum(buf.key);
if(num == 0){ //已经没有更高优先级的job了,可以打印
if(buf.flag == 1){ //如果是目标job则可以直接退出
break;
}else{
jobs.pop();
times++;
check[buf.key]--;
}
}else{ //还有更高优先级的,将其放入队尾
jobs.pop();
jobs.push(buf);
}
}
cout << times+1 << endl;
}
int main()
{
int T;
int buf;
cin >> T;
while (T>0){
memset(check, 0, sizeof(int)*10);
times = 0;
cin >> n >> m; //n:job数量;m:目标工作排号
for(int i=0; i<n; i++){
Job buf;
cin >> buf.key;
if(m == i)
buf.flag = 1; //将目标job做标记
else
buf.flag = 0;
jobs.push(buf);
check[buf.key]++; //记录该优先级拥有的数字值
}
comput();
while(jobs.size() != 0)
jobs.pop();
T--;
}
return 0;
}
Sicily 1443. Printer Queue
最新推荐文章于 2019-06-26 23:26:55 发布