P2278 [HNOI2003] 操作系统
#include<bits/stdc++.h>
using namespace std;
//1.怎样维护刚开始工作,还未工作完后来又被优先级更高的占了位置
//方法:将被抢占位置的元素持续时间加上时间tim,再减去后来元素开始的时间,得到的是还需要工作的时间
//2.怎样用堆维护结构体
//方法:将优先级作为第一关键字,开始时间作为第二关键字
/*bool operator < (const node &a)const{
if(pr==a.pr) return st>a.st;
else return pr<a.pr;
}*/
const int N=1e5+5;
struct node{
int num,st,re,pr;
bool operator<(node const &a)const{
if(pr==a.pr){
return st>a.st;
}else{
return pr<a.pr;
}
}
};
long long tim;
priority_queue<node> q;
int main(){
int x,n=0;
while(cin>>x){
node b;
b.num=x;
cin>>b.st>>b.re>>b.pr;
while(q.size()&&tim+q.top().re<=b.st){//如果队列中任务可以在当前任务开始之前结束
tim+=q.top().re;//更新结束时间
cout<<q.top().num<<" "<<tim<<"\n";
q.pop();
}
if(q.size()){//如果不能执行完毕
node c=q.top();
c.re=c.re-(b.st-tim);
q.pop();
q.push(c);
}
q.push(b);
tim=b.st;
}
while(q.size()){
tim+=q.top().re;
cout<<q.top().num<<" "<<tim<<"\n";
q.pop();
}
return 0;
}