bzoj 1216 操作系统

本题重点是模拟,坑了我两天,直到某位神犇给我指出了一个错误,也就是结构体里的Po变量,开始我没有定义这个变量。。。。。
下面介绍一下它的用处,记录这个标号的程序实际进入CPU处理的开始时间,与Begin变量不同,Begin只是为了排序,仅此而已,
而Po是为了处理实际剩余多少时间就可以运行完,千万不要将两者混用,否则就会WA
#include<cstdio>
#include<iostream>
#include<queue>
using namespace std;
int Ptime;
int A,B,C,D,charge;
int RT,n;
struct dd{
	int id,Begin,times,pri;
	friend bool operator < (dd a,dd b){
		if(a.pri==b.pri) return a.Begin>b.Begin;
		return a.pri<b.pri;
	}
}fu;
priority_queue<dd>que;
struct oo{
	int id,Begin,times,pri,Po;
}jie[300006];
void Work(int x){
    fu.id=x; fu.Begin=jie[x].Begin; fu.pri=jie[x].pri; que.push(fu);
}
int main(){
	freopen("system.in","r",stdin);
	freopen("system.out","w",stdout);
	while(scanf("%d%d%d%d",&A,&B,&C,&D)==4){
	  ++n; jie[n].id=A;jie[n].Begin=B; jie[n].Po=B; jie[n].times=C; jie[n].pri=D;
	}
	RT=1;
	Ptime=jie[1].Begin;
	for(int i=2;i<=n;++i){
	  if(Ptime+jie[RT].times<=jie[i].Begin){
			Ptime+=jie[RT].times;
			printf("%d %d\n",jie[RT].id,Ptime);
			if(!que.empty()&&jie[i].Begin>Ptime){//中间有时间空余,运行正在排队的;
				RT=que.top().id; que.pop(); i--; jie[RT].Po=Ptime;
				continue;
			}
			if(!que.empty()) {
				Work(i);//先进队,判断谁先运行;
			}
			if(que.empty()){//没有排队的,刚来的这个就进CPU运行;
				RT=i; Ptime=jie[i].Begin;
			}
			else{
               RT=que.top().id; que.pop();
               jie[RT].Po=Ptime;//取出队首,开始运行;
			}
			continue;
	  }
	  if(Ptime+jie[RT].times>jie[i].Begin&&jie[i].pri>jie[RT].pri){//优先级最高的先运行;
			jie[RT].times-=(jie[i].Begin-jie[RT].Po);
			Work(RT); Work(i);
            RT=que.top().id; que.pop(); Ptime=jie[i].Begin; jie[RT].Begin=Ptime;
			continue;
	  }
	  if(Ptime+jie[RT].times>jie[i].Begin&&jie[i].pri<=jie[RT].pri)
	      Work(i);
	}
	while(!que.empty()){
		Ptime+=jie[RT].times;
		printf("%d %d\n",jie[RT].id,Ptime);
		RT=que.top().id; que.pop();
	}
	printf("%d %d\n",jie[RT].id,Ptime+jie[RT].times);
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值