BZOJ 1216 优先队列

       这道题比较明显的是一道优先队列+模拟的题,优先队列就用STL里的然后自定义一个类型就好,主要是模拟的过程,只有我们处理到下一个进程的到达时间晚于我们当前处理进程执行完的时间,我们才能够确定这个进程能被执行完。对于执行了部分时间的进程,我们维护它的剩余时间并不断更新优先队列的值,然后模拟一下就出来了。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
struct Vergil
{
	int num,kth,arr,tex;	
	bool operator < (const Vergil &a) const
	{
		return (kth<a.kth||(kth==a.kth&&arr>a.arr));	
	}
};
priority_queue<Vergil> q;
int t,a,b,c,d;

void add(int a,int b,int c,int d)
{
	Vergil x;
	x.num=a;x.arr=b;
	x.tex=c;x.kth=d;
	q.push(x);
}

int main()
{
	scanf("%d%d%d%d",&a,&b,&c,&d);
	add(a,b,c,d);
	t=b;
	while (scanf("%d%d%d%d",&a,&b,&c,&d)==4) 
	{	
		Vergil x=q.top();q.pop();
		int v=x.tex;
		bool flag=0;
		while (t+v<=b) 
		{	
			printf("%d %d\n",x.num,t+v);
			t+=v;
			if (q.empty()) 
			{
				t=b;
				flag=1;
				break;
			}
			x=q.top();q.pop();
			v=x.tex;
		}
		add(a,b,c,d);
		if (flag) continue;
		x.tex-=(b-t);
		q.push(x);
		t+=(b-t);
	}
	while (!q.empty()) 
	{
		Vergil x=q.top();q.pop();
		int v=x.tex;
		printf("%d %d\n",x.num,t+v);
		t+=v;	
	}
	return 0;	
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值