对象存储调度问题(优先队列)(打卡2)

题目如下
在这里插入图片描述
最开始压根就没想到优先队列,意识到了贪心,但还是满脑子往动态规划那跑,结果…不用说了就写不出来。之后看了一些大佬的题解才知道用优先队列。
代码如下:

#include<cstdio>
#include<cctype>
#include<cstring>
#include<queue>
#include<algorithm>
#include<vector>
#include<utility> 
#include<iostream>
#define ll long long
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    ll T,n,m;
    ll t;
    cin >> T;
    while (T--)
    {
        priority_queue<ll, vector<ll>, less<ll>>q1, q2;
        cin >> n >> m;
        for (int i = 0; i < n; i++)
        {
            cin >> t;
            q1.push(t);
        }
        for (int i = 0; i < m; i++)
        {
            cin >> t;
            q2.push(t);
        }
        int flag = 0;
        while (!q1.empty())
        {
            if (q1.top() <= q2.top())
            {
                ll t2 = q2.top();
                q2.pop();
                t2 -= q1.top();
                q1.pop();
                q2.push(t2);
            }
            else
            {
                flag = 1;
                break;
            }
        }
        if (flag)
            cout << "No\n";
        else
            cout << "Yes\n";
    }
}

优先队列应该是个大顶堆,然后在定义的这块…
priority_queue<ll, vector, greater>q1, q2;
我一开始是这样写的,大嘛,就是greater,然后错了,想半天找了一个多小时才找到问题,因为HDU上不给测试样例有点让人难受。
大顶堆,从顶开始往下比就是less,嗯,就这样。
码一下,对于结构体的优先队列比较

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值