UVa 1153 Keep the Customer Satisfied (贪心+优先队列)

本文介绍了一个基于优先级的任务调度算法,用于解决多个串行任务在有限时间内最大化完成数量的问题。通过将任务按截止时间排序,并利用优先队列动态调整任务执行顺序,实现了高效的调度策略。

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

题目链接:https://vjudge.net/problem/UVA-1153

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

思路:将任务按照截止时间排序,对于每个任务,若满足要求就直接完成,若不满足要求,则从已完成任务中选取一个时间最长的取消,将当前的工作放进去,这个过程可以用优先队列来完成。


#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include<cctype>
#include <cstdlib>
#include<map>
#include<iostream>
#include<queue>
using namespace std;
typedef long long LL;
const int dr[] = {0,0,-1,1};
const int dc[] = {-1,1,0,0};
const int maxn = 800000 + 10;
struct Node
{
	int q, d;
	Node(int q = 0, int d = 0) : q(q), d(d) {}
	bool operator < (const Node& rhs) const {
		return d < rhs.d;
	}
}node[maxn];

struct cmp
{
	bool operator () (const Node& A, const Node& B) {
		return A.q < B.q;
	}
};

priority_queue<Node, vector<Node>, cmp> q;

int main()
{
	int T, n;
	scanf("%d", &T);
	while(T--)
	{
	   while(!q.empty()) q.pop();
       scanf("%d", &n);
       for(int i = 0; i < n; i++) scanf("%d%d", &node[i].q, &node[i].d);
       sort(node, node+n);
       int ans = 0, t = 0;
       for(int i = 0; i < n; i++) {
       	  if(t + node[i].q <= node[i].d) { ++ans; t += node[i].q; q.push(node[i]); }
       	  else if(!q.empty()) {
       	  	  int tt = t - q.top().q + node[i].q;
       	  	  if(tt < t) {
       	  	  	  q.pop();
       	  	  	  q.push(node[i]);
       	  	  	  t = tt;
       	  	  }
       	  }
       }
       printf("%d\n", ans);
       if(T) printf("\n");
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值