C.排队打饭
下课了,有n位同学陆续赶到食堂进行排队打饭,其中第i位同学的到达时间为ai,打饭耗时为ti,等待时间上限为bi,即如果其在第ai+bi秒的时刻仍然没有轮到他开始打饭,那么他将离开打饭队列另寻吃饭的地 。问每位同学的开始打饭时间,或者指出其提前离开了队伍(如果这样则输出-1)。
【输入格式】
第一行一个整数n(1<=n<=10^5),表示来打饭的同学数量。
接下来n ,每行三个整数ai,ti,bi(1<=ai,ti,bi<=10^9,1<=i<=n),分别表 每位同学的到达时间、打
饭耗时、等待时间上限。
保证a1
【输出格式】
n个整数,表示每位同学的开始打饭时间或者-1(如果该同学提前离开了队伍)。
【样例输 】
4
1 3 3
2 2 2
3 9 1
4 3 2
【样例输出】
1 4 -1 6
#include#include using namespace std; typedef struct { int ai;//到达时间 int ti;//打饭耗时 int bi;//等待时间上限 } student; int main(){ int n; cin >> n; vector stu; for(int i = 0; i < n; i++){ student s; cin >> s.ai >> s.ti >> s.bi; stu.push_back(s); } int now = stu[0].ai; for(int i = 0; i < n; i++){ //第一个学生铁定能打上饭 if(i == 0){ cout << stu[i].ai; now += stu[i].ti; } else{ //格式化输出,空格间隔 printf(" "); if(stu[i].ai + stu[i].bi < now) cout << -1; else{ //可能排着队紧挨着就到下一个同学了,也可能中间有空闲时间 cout << max(now,stu[i].ai); now = max(now,stu[i].ai) + stu[i].bi; } } } }



