UVA 1153 - Keep the Customer Satisfied(贪心)

本文介绍了一种通过排序和优先队列实现的订单调度算法,该算法能够在订单的时间限制内尽可能多地完成订单处理,通过替换已分配但价值较低的订单来提高整体效益。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:点击打开链接

思路:

我们按照时间从小到大排序, 因为时间最大2e6, 所以从小到大枚举时间, 当一个订单到了最后期限, 我们就看看它能否满足, 如果能就满足它, 如果不能, 我们看一下之前满足的订单中货物最多的一个是否比当前这个大, 如果大,就拿它换当前这个, 为后面节省空间。  用优先队列可以很方便的维护。

细节参见代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <ctime>
#include <bitset>
#include <cstdlib>
#include <cmath>
#include <set>
#include <list>
#include <deque>
#include <map>
#include <queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef long double ld;
const double eps = 1e-6;
const double PI = acos(-1);
const int mod = 1000000000 + 7;
const int INF = 0x3f3f3f3f;
const int seed = 131;
const ll INF64 = ll(1e18);
const int maxn = 1e6 + 10;
int T,n,m;
struct node {
    int v, t;
    node(int v=0, int t=0):v(v), t(t) {}
    bool operator < (const node& rhs) const {
        return t < rhs.t;
    }
}a[maxn];
int main() {
    scanf("%d",&T);
    while(T--) {
        scanf("%d", &n);
        int maxt = 0;
        priority_queue<int> q;
        for(int i = 1; i <= n; i++) {
            scanf("%d%d", &a[i].v, &a[i].t);
            maxt = max(maxt, a[i].t);
        }
        int cnt = 1;
        sort(a+1, a+n+1);
        int cur = 0, ans = 0;
        for(int i = 0; i <= maxt; i++) {
            if(i) ++cur;
            while(cnt <= n && a[cnt].t == i) {
                if(cur >= a[cnt].v) {
                    q.push(a[cnt].v);
                    cur -= a[cnt].v;
                    ++ans;
                }
                else if(!q.empty()) {

                    int hehe = q.top();
                    if(hehe > a[cnt].v) {
                        q.pop();
                        q.push(a[cnt].v);
                        cur += (hehe - a[cnt].v);
                    }
                }
                ++cnt;
            }
        }
        printf("%d\n", ans);
        if(T) printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值