Convention II(优先队列)

Farmer John的草食大会吸引了各地的牛,由于一种罕见的美味草,可能会导致牛排队等待。每头牛有计划的到达时间和品尝时间。我们需要计算出最大可能的等待时间。输入包含每头牛的到达时间和品尝时间,按照牛的优先级(到达顺序)确定谁先品尝。输出最长等待时间。示例中,最长等待时间为10单位时间。解决方案涉及使用优先队列来管理牛的等待和进食顺序。
摘要由CSDN通过智能技术生成

time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Despite long delays in airport pickups, Farmer John’s convention for cows interested in eating grass has been going well so far. It has attracted cows from all over the world.

The main event of the conference, however, is looking like it might cause Farmer John some further scheduling woes. A very small pasture on his farm features a rare form of grass that is supposed to be the tastiest in the world, according to discerning cows. As a result, all of the N cows at the conference (1≤N≤105) want to sample this grass. This will likely cause long lines to form, since the pasture is so small it can only accommodate one cow at a time.

Farmer John knows the time ai that each cow i plans to arrive at the special pasture, as well as the amount of time ti she plans to spend sampling the special grass, once it becomes her turn. Once cow i starts eating the grass, she spends her full time of ti before leaving, during which other arriving cows need to wait. If multiple cows are waiting when the pasture becomes available again, the cow with the highest seniority is the next to be allowed to sample the grass. For this purpose, a cow who arrives right as another cow is finishing is considered “waiting”. Similarly, if a number of cows all arrive at exactly the same time while no cow is currently eating, then the one with highest seniority is the next to eat.

Please help FJ compute the maximum amount of time any cow might possibly have to wait in line (between time ai and the time the cow begins eating).

Input
The first line of input contains N. Each of the next N lines specify the details of the N cows in order of seniority (the most senior cow being first). Each line contains ai and ti for one cow. The ti’s are positive integers each at most 104, and the ai’s are positive integers at most 109.

Output
Please print the longest potential waiting time over all the cows.

Example
inputCopy
5
25 3
105 30
20 50
10 17
100 10
outputCopy
10
Note
In this example, we have 5 cows (numbered 1…5 according to their order in the input). Cow 4 is the first to arrive (at time 10), and before she can finish eating (at time 27) cows 1 and 3 both arrive. Since cow 1 has higher seniority, she gets to eat next, having waited 2 units of time beyond her arriva

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值