12100 Printer Queue
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 soon.
• One line with 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 soon.
Output
For each test case,print one line with a single integer;the number of minutes until your job is completely
printed,as suming 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
用到了关系运算符的重载
关系运算符的定义
关系运算符有==,!=,<,>,<=,>=。
bool operator == (const A& );bool operator != (const A& );bool operator < (const A& );bool operator <= (const A& );bool operator > (const A& );bool operator >= (const A& );
还有就是优先队列的使用,因为是结构体所以要用到运算符重载。。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
struct node
{
int num,level;
bool operator<(const node &a)const//关系运算符<的重载
{
return level<a.level;
}
};
int main()
{
int t;
cin>>t;
while(t--)
{
queue<node>q;
priority_queue<node>qq;
struct node a;
int n,m;
cin>>n>>m;
int i;
for(i=0;i<=n-1;i++)
{
scanf("%d",&a.level);
a.num=i;
q.push(a);
qq.push(a);
}
int ans=0;
while(1)
{
if(qq.top().level==q.front().level)
{
ans++;
a=q.front();
qq.pop();
q.pop();
if(a.num==m) break;
}
else
{
a=q.front();
q.pop();
q.push(a);
}
}
cout<<ans<<endl;
}
return 0;
}
本文介绍了一种解决打印机队列问题的方法,通过使用优先队列和重载关系运算符来确定打印作业完成的时间。示例输入输出展示了算法的有效性。
436

被折叠的 条评论
为什么被折叠?



