现在的位置: 首页 > 综合 > 正文

hoj Printer Queue

2013年01月30日 ⁄ 综合 ⁄ 共 567字 ⁄ 字号 评论关闭
/*这是一道用循环队列和优先队列模拟的题。
用结构体来定义队列。使队列能够能够记录元素的值和下标。
优先队列入队的同时已经自动进行排序。

top()的值都为当前队列的最大值。

具体模拟过程见程序

*/
#include <iostream>
#include <queue>
#include <cstring>
struct Work
{
    int in;
    int pos;
} work[110];
using namespace std;


int main()
{
    int a[110];
    int t,n,m;
    cin >> t;
    while(t--)
    {
        priority_queue<int> pri;
        queue<Work> p;
        cin >> n >> m;
        memset(a,0,sizeof(a));
        for(int i=0; i<n; i++)
        {
            cin >> a[i];
            work[i].pos=a[i];
            work[i].in=i;
            pri.push(a[i]);
            p.push(work[i]);
        }
        int count =0;
        while(1)
        {
            if(pri.top()==p.front().pos)
            {
                count++;
                if(p.front().in==m)
                    break;
                pri.pop();
                p.pop();
            }
            else
            {
                p.push(p.front());
                p.pop();
            }
        }
        cout << count  << endl;
    }
    return 0;
}

抱歉!评论已关闭.