UVa12100

来源:互联网 发布:淘宝免费店招图片 编辑:程序博客网 时间:2024/06/05 10:24

题意:给出n个有优先级的job,9表示优先级最高,1表示优先级最低,如果队列中有比队首优先级更高的job,将队首的job移到队尾,否则移出队首job打印

思路:用优先级队列来排序,将优先级最高的job放到 队首,用普通队列来存取job的顺序。

代码 如下:

#include <iostream>#include <fstream>#include <queue>class Solution{public:    void init(int n, int m)    {        this-> n = n; this->m = m;    }    void setA(int i, int n)    {        a[i] = n;    }    int solve()    {        std::queue<Node> queue;        std::priority_queue<int> pq;        for (int i = 0; i < n; i++)        {            Node node;            node.pos = i; node.num = a[i];            queue.push(node);            pq.push(a[i]);        }        int time = 0;        while (!queue.empty())        {            Node node = queue.front();            if (node.num >= pq.top())            {                time++;                pq.pop();                queue.pop();                if (node.pos == m) break;            }            else            {                queue.pop();                queue.push(node);            }        }        return time;    }private:    class Node    {    public:        int pos;        int num;        bool operator < (Node& b) const        {            return num < b.num;        }    };private:    static const int N = 101;    int a[N];    int n, m;};int main() {#ifndef ONLINE_JUDGE    std::ifstream fin("f:\\oj\\uva_in.txt");    std::streambuf* old = std::cin.rdbuf(fin.rdbuf());#endif    int t;    Solution solver;    std::cin >> t;    for (int i = 1; i <= t; i++)    {        int n, m;        std::cin >> n >> m;        solver.init(n, m);        for (int i = 0; i < n; i++)        {            int a;            std::cin >> a;            solver.setA(i, a);        }        int ans = solver.solve();        std::cout << ans << std::endl;    }#ifndef ONLINE_JUDGE    std::cin.rdbuf(old);#endif    return 0;