紫书第五章习题 5-7 UVA12100-打印队列

出处:https://blog.csdn.net/richr_wong/article/details/50190371

题目链接:https://vjudge.net/contest/231030#problem/G

题目大意:

现在有一个打印队列,里面的任务是无序的,每个任务有一个权值,打印机会从第一个任务开始执行,如果当前任务的权值是队列中最大的,那么就打印,否则就将该任务放置到队末。每次打印消耗一定的时间,求该序列中的任务分别在什么时候被打印。

这题开一个优先队列一个普通队列,队列里的元素出队,如果出队元素和优先队列队首元素相同就执行“打印”操作,否则就扔到队尾。

#include <iostream>  
#include <string>  
#include <queue>  
using namespace std;  
int main()  
{  
    int n;  
    cin>>n;  
    for(int i=0;i<n;i++)  
    {  
        int num, index;  
        queue<int> q;  
        priority_queue<int> pq;  
        cin>>num>>index;  
        for(int j=0;j<num;j++)  
        {  
            int rate;  
            cin>>rate;  
            pq.push(rate);  
            q.push(rate);  
        }  
        int x = 0;  
        while(true)  
        {  
            if(q.front()==pq.top())  
            {  
                if(x==index)  
                {  
                    cout<<num-q.size()+1<<endl;  
                    break;  
                }  
                else  
                {  
                    q.pop();  
                    pq.pop();  
                    x++;  
                }  
            }  
            else  
            {  
                int temp = q.front();  
                q.pop();  
                q.push(temp);  
                if(x==index)  
                {  
                    x=0;  
                    index=q.size()-1;  
                }  
                else  
                {  
                    x++;  
                }  
            }  
        }  
    }  
    return 0;  
}  

猜你喜欢

转载自blog.csdn.net/JXUFE_ACMer/article/details/80454870