您的位置:首页 > 产品设计 > UI/UE

1443. Printer Queue(用一个数组模拟队列,优先级高的先出队列)

2013-01-04 15:22 260 查看
/*1443. Printer Queue(用一个数组模拟队列,优先级高的先出队列)
题目大意:给出一串数字,代表打印队列里面的各个作业的
优先级,数字越大优先级越高。同时给出一个指定
作业位置下标。
在队列中,优先打印高优先级的作业,若高优先级
在队列头则出队即可,若否,则队列头放到最后,同
时高优先级的放到头出队。
求 指定的作业经过多少分钟才能完成。

方法:用一个节点数组模拟队列。先获得队列头,然后跟后面的各个
节点的优先级比较,若发现比它大的则将其往后移,队列头往后
一位,再进行遍历比较。若当前的是最大的,则出队即可。
出队时判断是否指定作业,是则输出耗时,否则继续。

*/

#include <stdlib.h>
#include <iostream>
#include <deque>

using namespace std;

struct node
{
int index;
int prio;
};

int main()
{
int N;
cin >> N;
int n, index;
int prio;
node pp[200000];
while (N--)
{
cin >> n >> index;
int head=0;
int rail=n-1;
for(int i=0;i<n; i++)
{
cin >> prio;
node temp = {i, prio};
pp[i] = temp;
}
int time = 0;
while(true)
{
node temp = pp[head];
head++;
int i;
for(i=head; i<=rail; i++)
if(pp[i].prio > temp.prio)
break;

if(i>rail)
{
time++;
if(temp.index == index)
{
cout << time << endl;
break;
}
}
else
{
pp[++rail] = temp;
}

}

}

system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: