UVA 1153 Keep the Customer Satisfied(顾客是上帝)(贪心)

题意:有n(n<=800000)个工作,已知每个工作需要的时间qi和截止时间di(必须在此之前完成),最多能完成多少个工作?工作只能串行完成。第一项任务开始的时间不早于时刻0。

分析:按截止时间排序,在若当前工作完成后超过截止时间,则优先选择需要时间少的工作,即若优先队列中需要时间最长的工作a,

1、其时间大于当前工作b,则选择完成工作b,弹出a。(此操作的前提是完成b弹出a后的时间要小于等于b的截止时间)

2、其时间小于当前工作b,则没必要弹出a。

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
const double eps = 1e-8;
inline int dcmp(double a, double b) {
    if(fabs(a - b) < eps)  return 0;
    return a < b ? -1 : 1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN =  800000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
struct Node{
    int q, d;
    void read(){
        scanf("%d%d", &q, &d);
    }
    bool operator < (const Node& rhs)const{
        return d < rhs.d;
    }
}num[MAXN];
int n;
int solve(){
    priority_queue<int> q;
    int st = 0;
    for(int i = 0; i < n; ++i){
        if(st + num[i].q <= num[i].d){
            st += num[i].q;
            q.push(num[i].q);
        }
        else if(!q.empty()){
            int tmp = q.top();
            if(tmp > num[i].q && st - tmp + num[i].q <= num[i].d){
                q.pop();
                q.push(num[i].q);
                st += num[i].q - tmp;
            }
        }
    }
    return q.size();
}
int main(){
    int T;
    scanf("%d", &T);
    while(T--){
        scanf("%d", &n);
        for(int i = 0; i < n; ++i) num[i].read();
        sort(num, num + n);
        printf("%d\n", solve());
        if(T) printf("\n");
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/6383169.html