您的位置:首页 > 其它

UVA 12100

2016-02-11 00:01 190 查看
题意:有一个打印机,有一些任务在排着队打印,每个任务都有优先级。打印时,每次取出队列第一个任务,如果它的优先级不是当前队列中最高的,就会被放到队尾,否则就打印出来。输出初始队列的第m个任务的打印时间,每次打印花费单位1的时间。

思路:用队列模拟一下。建一个<num,pos>的队列q,num表示优先级,pos表示位置。然后再按照优先级建一个优先队列pq。

每次先把q的队首拿出来

(1)不是当前优先级最高的,num<pq.top(),再把出队的这个元素push回去。

(2)是当前优先级最高的,num==pq.top()

如果pos == m ,输出结束。


#include <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <utility>
using namespace std;

#define rep(i,j,k) for (int i=j;i<=k;i++)
#define Rrep(i,j,k) for (int i=j;i>=k;i--)

int T;
int n,m,temp;

struct node
{
int num,pos;
node(int x = 0,int y = 0)
{
num = x;
pos = y;
}
};
int main()
{
cin>>T;
while(T--)
{
queue<node> q;
priority_queue<int> pq;
cin>>n>>m;
rep(i,0,n-1)
{
scanf("%d",&temp);
q.push(node(temp,i));
pq.push(temp);
}
int t;
node x;
int ans = 0;
while(1)
{
x = q.front();
t = pq.top();
q.pop();
if ( t == x.num )
{
pq.pop();
ans++;
if ( x.pos == m )
{
cout<<ans<<endl;
break;
}
}
else q.push(x);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva