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 ts minutes have passed after midnight and closes after tf minutes have passed after midnight (so that (tf - 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://i-blog.csdnimg.cn/blog_migrate/a2c970de921b4c910d92380fb3946241.png)
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 ts, the point of time when the receptionist stops working tf and the time the receptionist spends on each visitor t. The second line contains one integer n — the amount of visitors (0 ≤ n ≤ 100 000). 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 1012; it is guaranteed that ts < tf. 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.
10 15 2 2 10 13
12
8 17 3 4 3 4 5 8
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.
题目大意:首先吐槽一下题目很长,,,意思很繁杂。就是一个人想去办理事情,想等待最少的时间去办理,现在他知道当天办公室的开门时间和工作人员对于每个人的接待时间和他们的工作时间,然后他还知道当天去办理业务的每个人的到达时间,如果他和其他人在同时到达那里的话,他需要排在最后去办理,还有就是工作人员在剩余工作时间不足以完成一个人的业务办理的时候,他不会给新来的人办理业务。
题目分析:意思理解完了之后,就是有点模拟了,不过自我感觉这道题目特别考验思维的转折,因为给你的数列是一个非递减的数列,所以你只需要从头开始遍历就行了,然后对于每个人的到达时间,你选择是否在他们前面一分钟到达并且工作人员可以帮助下一位人处理完事情,并且你还需要更新最短的用时,当然不能忘记,这个最短的等待时间是有可能是0分钟的。
#include <bits/stdc++.h>
using namespace std;
#define maxn 1e12+7
long long m=maxn;
long long k,a,n,ts,tf,t,ans;
int main(){
cin>>ts>>tf>>t;
cin>>n;
while(n--){
cin>>k;
if(k<=tf-t){//判断工作人员是否在剩余的时间完成业务办理
if(k && max(ts,k-1)<=tf-t && ts-k+1 < m ){//ts是不断更新的工作人员可以开始办理下一个业务的时间,
//m是当前需要等待的最短时间。
m=ts-k+1;
ans=min(ts,k-1);
}
ts=max(ts,k)+t;//不断更新ts
}
}
if(ts <= tf-t) ans=ts;//判断是否可以在最后不需要等待直接办理
cout<<ans<<endl;
return 0;
}