UVA 12100 Printer Queue(队列,优先队列)

朱雀 2022-06-07 02:26 296阅读 0赞

详细题目:https://vjudge.net/problem/UVA-12100

思路:

用队列模拟,开一个 的队列,分别表示任务的优先级和位置。再开一个 的优先队列,默认任务的优先级为从大到小的优先顺序。

每次取出队列首任务,若其优先级不是最高的(根据优先队列判断),将其入队;否则,ans++,再判断其位置是否为所求任务完成的时刻,若是则输出ans结束。

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <queue>
  4. using namespace std;
  5. struct node
  6. {
  7. int pri,pos;
  8. };
  9. int main()
  10. {
  11. int t;
  12. cin>>t;
  13. while(t--)
  14. {
  15. queue<node> q;
  16. priority_queue<int> pq;
  17. int n,k;
  18. cin>>n>>k;
  19. for(int i=0;i<n;i++)
  20. {
  21. node a;
  22. cin>>a.pri;
  23. a.pos=i;
  24. q.push(a);
  25. pq.push(a.pri);
  26. }
  27. node x;
  28. int ans=0,tp;
  29. while(1)
  30. {
  31. x=q.front();
  32. q.pop();
  33. tp=pq.top();
  34. if(x.pri==tp)
  35. {
  36. pq.pop();
  37. ans++;
  38. if(x.pos==k)
  39. {
  40. cout<<ans<<endl; break;
  41. }
  42. }
  43. else
  44. q.push(x);
  45. }
  46. }
  47. return 0;
  48. }

发表评论

表情:
评论列表 (有 0 条评论,296人围观)

还没有评论,来说两句吧...

相关阅读

    相关 优先队列(priority_queue)

    优先队列:优先级最高的先出队。 队列和排序的完美结合,可以存储数据,还可以把这些数据按设定的规则进行排序。 每次的push和pop操作,优先队列都会动态调整,把优先级最