bzoj1216(堆,呵呵的模拟)

本文介绍了一种使用堆数据结构的应用技巧,重点在于如何通过维护进程的优先级队列来高效处理任务调度问题。文章详细解释了如何利用优先队列进行进程调度,并提供了完整的代码示例。

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

就是堆的应用,实现的时候会有点奇怪的技巧

now作为当前时间,顺序枚举每一个进程的到来,注意在每一个进程来之前要处理完成前面的所有进程!


注意放一个哨兵元素lev为-inf,rest为inf,防止bug,

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<cstdlib>
#include<queue>
using namespace std;
const int inf=0x3f3f3f3f;
inline int read()
{
	int ans,f=1;char ch;
	while ((ch=getchar())<'0'||ch>'9') if (ch=='-') f=-1;ans=ch-'0';
	while ((ch=getchar())>='0'&&ch<='9') ans=ans*10+ch-'0';
	return ans*f;
}

struct aa
{
	int id,ar,rest,lev;
	bool operator <(const aa &b) const
	{
		if (lev==b.lev) return ar>b.ar; 
		else return lev<b.lev;
	} 
}a[800005];
int tot;

priority_queue<aa> heap;

int main()
{
	int x,y,z,w;
	while (scanf("%d%d%d%d",&x,&y,&z,&w)!=EOF)
	{
		tot++;
		a[tot].id=x;a[tot].ar=y;
		a[tot].rest=z;a[tot].lev=w;
	}
	int now=0;
	aa tmp;
	tmp.rest=inf;
	tmp.lev=-inf;
	heap.push(tmp);
	
	for (int i=1;i<=tot;i++)
	{
		int now1=a[i].ar;
		while (heap.top().lev!=-inf&&heap.top().rest<=now1-now)
		{
			now+=heap.top().rest;
			printf("%d %d\n",heap.top().id,now);
			heap.pop();
		}
		aa v=heap.top();
		heap.pop();
		v.rest-=now1-now;
		heap.push(v);
		
		heap.push(a[i]);
		now=now1;
	}
	
	while (heap.top().lev!=-inf)
	{
		now+=heap.top().rest;
		printf("%d %d\n",heap.top().id,now);
		heap.pop();
	}
	
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值