洛谷P2278 [HNOI2003]操作系统
1 #include<cstdio> 2 #include<iostream> 3 #include<queue> 4 using namespace std; 5 struct data{ 6 int ID,Time_A,Time_B,rank; 7 inline data(int ID=0,int Time_A=0,int Time_B=0,int rank=0): 8 ID(ID),Time_A(Time_A),Time_B(Time_B),rank(rank){} 9 inline bool operator < (const data &b)const {return rank<b.rank||(rank==b.rank&&Time_A>b.Time_A);} 10 }Now; 11 priority_queue<data> q; 12 int nowt;//队列中上一个结束时间 13 int main() 14 { 15 while(scanf("%d%d%d%d",&Now.ID,&Now.Time_A,&Now.Time_B,&Now.rank)!=EOF)//读入新任务 16 { 17 while(!q.empty()&&nowt+q.top().Time_B<=Now.Time_A)//之前任务能做完 18 { 19 nowt+=q.top().Time_B;//更新时间 20 printf("%d %d\n",q.top().ID,nowt); 21 q.pop(); 22 } 23 if(!q.empty())//队列中仍有任务做不完 24 { 25 data Top=q.top(); 26 Top.Time_B-=Now.Time_A-nowt;//将其持续时间缩短 27 q.pop();q.push(Top); 28 } 29 q.push(Now);nowt=Now.Time_A;//把新任务放入队列 更新当前时间 30 } 31 while(!q.empty()) 32 { 33 data Top=q.top();q.pop();nowt+=Top.Time_B;//不断更新当前时间 34 printf("%d %d\n",Top.ID,nowt); 35 } 36 return 0; 37 }