303988: CF767B. The Queue

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

The Queue

题意翻译

终于!$Vasya$ 成年了,这意味着他终于可以拿到护照了!要做到这一点,他需要访问护照办公室,但这不是那么简单。 护照办公室里只有一个接待员,人们可以在它开始服务之前排队等候。$Vasya$ 想明天去护照办公室。他知道接待员在午夜$Ts$ 分钟时开始工作,并在午夜$Tf$ 分钟后关闭。接待员的最后一分钟仍在工作。他对排队的每个人都花了不到几分钟的时间。如果接待员在$Tt$ 分钟内停止工作,他就不再为访客服务(除了他已经服务过的客人)。$Vasya$ 也知道明天会造访的访问者的个数。对于每个来访者,$Vasya$ 都知道他何时会来到护照办公室。每个访客排队等候,直到被服务才离开。如果接待员闲着而一个来访者走了进来,他就立刻开始为客人服务。 $Vasya$ 可以在零的时候到办公室(也就是在午夜),如果他乐意的话。但是他只能在整数的时间到达办公室。如果$Vasya$ 和其他几个来访者同时到达护照办公室,他会向他们让步,并作为最后一名之后站在队伍中。 $Vasya$ 想找到一个适合的时间,使得接待员会服务他,并且他会花费最少的时间在队伍中。请你帮帮他。

题目描述

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 $ t_{s} $ minutes have passed after midnight and closes after $ t_{f} $ minutes have passed after midnight (so that $ (t_{f}-1) $ is the last minute when the receptionist is still working). The receptionist spends exactly $ t $ minutes on each person in the queue. If the receptionist would stop working within $ t $ minutes, he stops serving visitors (other than the one he already serves). 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. ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF767B/21e6fa9f8076fbac8e8f856cf43d41d3ac4208dc.png)"Reception 1"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 $ t_{s} $ , the point of time when the receptionist stops working $ t_{f} $ and the time the receptionist spends on each visitor $ t $ . The second line contains one integer $ n $ — the amount of visitors ( $ 0<=n<=100000 $ ). The third line contains positive integers in non-decreasing order — the points of time when the visitors arrive to the passport office. All times are set in minutes and do not exceed $ 10^{12} $ ; it is guaranteed that $ t_{s}&lt;t_{f} $ . It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist.

输出格式


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.

输入输出样例

输入样例 #1

10 15 2
2
10 13

输出样例 #1

12

输入样例 #2

8 17 3
4
3 4 5 8

输出样例 #2

2

说明

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.

Input

题意翻译

终于!$Vasya$ 成年了,这意味着他终于可以拿到护照了!要做到这一点,他需要访问护照办公室,但这不是那么简单。 护照办公室里只有一个接待员,人们可以在它开始服务之前排队等候。$Vasya$ 想明天去护照办公室。他知道接待员在午夜$Ts$ 分钟时开始工作,并在午夜$Tf$ 分钟后关闭。接待员的最后一分钟仍在工作。他对排队的每个人都花了不到几分钟的时间。如果接待员在$Tt$ 分钟内停止工作,他就不再为访客服务(除了他已经服务过的客人)。$Vasya$ 也知道明天会造访的访问者的个数。对于每个来访者,$Vasya$ 都知道他何时会来到护照办公室。每个访客排队等候,直到被服务才离开。如果接待员闲着而一个来访者走了进来,他就立刻开始为客人服务。 $Vasya$ 可以在零的时候到办公室(也就是在午夜),如果他乐意的话。但是他只能在整数的时间到达办公室。如果$Vasya$ 和其他几个来访者同时到达护照办公室,他会向他们让步,并作为最后一名之后站在队伍中。 $Vasya$ 想找到一个适合的时间,使得接待员会服务他,并且他会花费最少的时间在队伍中。请你帮帮他。

加入题单

上一题 下一题 算法标签: