P2278
#include <cstdio>
#include <queue>
struct task
{
int number;
int need;
int get;
int grade;
inline bool operator < (const task &b) const
{
return this->grade<b.grade||(this->grade==b.grade&&this->get>b.get);
}
};
std::priority_queue<task,std::vector<task>,std::less<task>> wait;
int main()
{
int num,get,times,grade;
int time=-1;
while(scanf("%d%d%d%d",&num,&get,×,&grade)!=EOF)
{
while((!wait.empty())&&time+wait.top().need<=get)
{
time+=wait.top().need;
printf("%d %d\n",wait.top().number,time);
wait.pop();
}
if(!wait.empty())
{
task cache=wait.top();
wait.pop();
cache.need-=get-time;
wait.push(cache);
}
wait.push({num,times,get,grade});
time=get;
}
while(!wait.empty())
{
time+=wait.top().need;
printf("%d %d\n",wait.top().number,time);
wait.pop();
}
return 0;
}