Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it’s not that simple. There’s only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow.
He knows that the receptionist starts working after
Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn’t leave until he was served. If the receptionist is free when a visitor comes (in particular, if the PRevious visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately.
For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them.
Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him!
The first line contains three integers: the point of time when the receptionist begins to work
All times are set in minutes and do not exceed
Print single non-negative integer — the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them.
input
10 15 2210 13output
12input
8 17 343 4 5 8output
2Note
In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight.
In the second example, Vasya has to come before anyone else to be served.
Vasya 准备排队去办理护照。已知只有一个窗口在提供服务,且提供服务的时间在
貌似这是我打 CF 以来经历的最坑的 B 题。
首先进行预处理,已知每个人进入队列的时间 s[i] ,当不考虑 Vasya 排队的情况,每人结束服务的时间 e[i] 可通过前一个人的结束时间获得。若前一个人的结束时间 e[i-1]+1 >= s[i]
,则当前这个人的结束时间记为 e[i] = e[i-1] + t
。否则可以认为在 e[i-1]+1
时刻队列为空, Vasya 在此时来办理业务无需等待。
在处理完 e[]
数组后,尝试在每一个人开始时间前 1 时刻 s[i]-1
让 Vasya 去排队,计算他需要等待(此处加上了他办理业务的用时)的时间为 e[i]-(s[i]-1)+1 = e[i]-s[i]+2
。取最小的等待时间对应的 s[i]-1
即可。
综合上述想法,仍然有 n=0
的情况以及在处理完最后一个人的业务后仍有充裕时间供 Vasya 办理业务的情况。特判。
新闻热点
疑难解答