UVa978 - Lemmings Battle!(优先级队列)

题意:给出两个队及每个队员的战斗力,b个战场,每一轮从两个队中选中b个职员来打斗,赢的队员进入下一轮,直到其中的一个队赢或者打平。

思路:将两个队放入两个优先级队列中,每次从两个队列中取出b个元素,放入到两上数组中,将两个数组进行比较,将不为0的数组元素重新加入队列中。

代码如下:

#include 
#include 
#include 
#include 

class Solution
{
public:
    void init(int b, int sg, int sb)
    {
        m_b = b; m_sg = sg; m_sb = sb;

        while (!g_queue.empty()) g_queue.pop();
        while (!b_queue.empty()) b_queue.pop();
    }

    void add_g(int num)
    {
        g_queue.push(num);
    }

    void add_b(int num)
    {
        b_queue.push(num);
    }

    void solve()
    {
        std::vector vg(m_b), vb(m_b);
        while (!g_queue.empty() && !b_queue.empty())
        {
            for (int i = 0; i < m_b; i++)
            {
                if (g_queue.empty()) break;
                int num = g_queue.top(); g_queue.pop();
                vg[i] = num;
            }

            for (int i = 0; i < m_b; i++)
            {
                if (b_queue.empty()) break;
                int num = b_queue.top(); b_queue.pop();
                vb[i] = num;
            }

            for (int i = 0; i < m_b; i++)
            {
                if (vg[i] > vb[i])
                {
                    vg[i] -= vb[i];
                    vb[i] = 0;
                }
                else if (vg[i] < vb[i])
                {
                    vb[i] -= vg[i];
                    vg[i] = 0;
                }
                else
                {
                    vg[i] = vb[i] = 0;
                }
            }

            for (int i = 0; i < m_b; i++)
            {
                if (vg[i] != 0)
                {
                    g_queue.push(vg[i]);
                    vg[i] = 0;
                }

                if (vb[i] != 0)
                {
                    b_queue.push(vb[i]);
                    vb[i] = 0;
                }
            }
        }

        if (g_queue.empty() && b_queue.empty())
        {
            std::cout << "green and blue died" << std::endl;
        }
        else if (g_queue.empty())
        {
            std::cout << "blue wins" << std::endl;
            while (!b_queue.empty())
            {
                int num = b_queue.top(); b_queue.pop();
                std::cout << num << std::endl;
            }
        }
        else
        {
            std::cout << "green wins" << std::endl;
            while (!g_queue.empty())
            {
                int num = g_queue.top(); g_queue.pop();
                std::cout << num << std::endl;
            }
        }

    }

private:
    int m_b, m_sg, m_sb;
    std::priority_queue g_queue, b_queue;
};


int main() {
#ifndef ONLINE_JUDGE
    std::ifstream fin("e:\\program\\clion\\uva_in.txt");
    std::streambuf* old = std::cin.rdbuf(fin.rdbuf());
#endif

    int n;
    std::cin >> n;
    Solution solver;
    while (n--)
    {
        int b, sg, sb;
        std::cin >> b >> sg >> sb;
        solver.init(b, sg, sb);
        for (int i = 0; i < sg; i++)
        {
            int num;
            std::cin >> num;
            solver.add_g(num);
        }

        for (int i = 0; i < sb; i++)
        {
            int num;
            std::cin >> num;
            solver.add_b(num);
        }

        solver.solve();
        if (n) std::cout << std::endl;
    }

#ifndef ONLINE_JUDGE
    std::cin.rdbuf(old);
#endif
    return 0;
}


你可能感兴趣的:(#,uva)