题解 | #公司食堂#

公司食堂

http://www.nowcoder.com/practice/601815bea5544f389bcd20fb5ebca6a8

刚开始用队列实现的,多了排序,怎么都超时,看来别人的思路,用了优先队列,立马就过了。

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <functional>
#include <cstdlib>
#include <vector>
#include<bits/stdc++.h>

using namespace std;

int main()
{
    int t, n, m;
    string d;
    string p;
    cin >> t;
    while (t--)
    {
        cin >> n >> d >> m >> p;
        
        // 优先级队列(小根堆)
        priority_queue<int,vector<int>,greater<int>> zero, one;
        for (int j = 0; j < n; j++)
        {
            if (d[j] == '0')
            {
                zero.push(j + 1);
            }
            else if (d[j] == '1')
            {
                one.push(j + 1);
            }
        }
        for (int i = 0; i < m; i++)
        {
            if (p[i] == 'M')
            {
                // 如果没有仅一人的座位
                if (one.empty())
                {
                    cout << zero.top() << '\n';
                    one.push(zero.top());
                    zero.pop();
                }
                else
                {
                    cout << one.top() << '\n';
                    one.pop();
                }
            }
            else
            {
                // 如果没有空位置
                if (zero.empty())
                {
                    cout << one.top() << '\n';
                    one.pop();
                }
                else
                {
                    cout << zero.top() << '\n';
                    one.push(zero.top());
                    zero.pop();
                }
            }
        }
    }
}
全部评论
我Java用优先队列依然超时,离谱的很
点赞 回复 分享
发布于 2022-10-25 21:08 广东

相关推荐

迟缓的斜杠青年巴比Q了:简历被投过的公司卖出去了,我前两天遇到过更离谱的,打电话来问我有没有意向报班学Java学习,服了,还拿我学校一个学长在他们那报班学了之后干了华为OD当招牌
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务