UVa 1153 顾客是上帝(Keep the Customer Satisfied)

该博客分析了如何使用优先队列解决UVa 1153问题,即在给定每个工作的时间需求和截止日期的情况下,确定最多能完成多少个工作。工作按截止日期排序,确保优先处理时间更紧迫的任务。文章提供了解决方案的代码实现。

题意:
有n个工作,已知每个工作需要的时间q和截止时间d,最多能完成多少个工作?工作只能一个一个完成,起始时间从0开始。

分析:
以d排序,优先队列。
可以保证永远都是符合条件的时间小的在优先队列中。

代码:

#include<bits/stdc++.h>
#define LL long long
#define ms(s) memset(s, 0, sizeof(s))
using namespace std;
const int maxn = 1e6 + 10;

struct Node {
    int p, d;
    friend bool operator < (const Node& n1, const Node& n2) {
        return n1.d < n2.d;
    }
}node[maxn];

int main() {
    // freopen("in.txt", "r", stdin);
    // freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    int T;
    cin >> T;
    int kase = 0;
    while(T--) {
        if(kase++) std::cout << endl;
        int n;
        cin >> n;
        for(int i = 0; i < n; i++) {
            cin >> node[i].p >> node[i].d;
        }
        sort(node, node + n);
        priority_queue<int> pq;
        int cur = 0;
        for(int i = 0; i < n; i++) {
            if(node[i].p + cur <= node[i].d) {
                cur += node[i].p;
                pq.push(node[i].p);
            } else {
                if(pq.empty()) continue;
                int v = pq.top();
                if(v >= node[i].p) {
                    pq.pop();
                    cur -= v - node[i].p;
                    pq.push(node[i].p); 
                }
            }
        }
        cout << pq.size() << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值