UVa #1153 Keep the Customer Satisfied (习题8-12)

本文提供了一种解决UVa 8-12.1153问题的有效算法实现,该算法通过优先队列来管理任务并确保每个任务在其截止日期前完成。讨论了样例输出格式的重要性以及题目描述中容易忽视的细节。

太担心于算法的正确性,一不留神被输出的空行要求坑了。凡是只给一组sample的,都应该首先留意输出格式的文字要求


另外,题目的"before its due date"也是坑啊,加一个"or on"会怀孕么



Run Time: 0.399s

#define UVa  "8-12.1153.cpp"		//Keep the Customer Satisfied
char fileIn[30] = UVa, fileOut[30] = UVa;

#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>

using namespace std;

struct Job {
    int q, d;
    Job(int a, int b):q(a),d(b){}
    bool operator < (const Job& j) const {
        return d < j.d;
    }
};

//Global Variables. Reset upon Each Case!
const int maxn = 800000 + 10;
int T, n;
/////

int dp(int i, int j);
int main() {
    scanf("%d", &T);
    while(T--) {
        priority_queue<int> ans;
        vector<Job> v;
        scanf("%d", &n);
        int a, b;
        for(int i = 0; i < n; i ++) {
            scanf("%d%d", &a,&b);
            v.push_back(Job(a,b));
        }
        sort(v.begin(), v.end());
        long long t = 0;
        for(int i = 0; i < n; i ++) {
            Job& u = v[i];
            if (!ans.empty() && t + u.q > u.d && ans.top() > u.q) {       //not enough time.
                t -= ans.top();
                ans.pop();
            }
            if(t + u.q <= u.d) {
                t += u.q;
                ans.push(u.q);
            }
        }
        printf("%d\n", ans.size());
        if(T) printf("\n");
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值