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.
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.
解题思路:
1、首先进行情况的特判
2、贪心思想,如果想利用足够小的时间得到服务,在原来的队伍中进行插队,只需比后面那一个人早到一分钟即可。这样就可以进行判断。先用一个数组保存,在第k个人时,前k-1人的得到服务后的时间。如果在第k个人要服务时,前k-1个人都已经服务完毕,则直接输出 k到达时间-1 表示其不需要等待,否则其需要等待 sum[k] - k + 1 时间。
然后暴力1e5 就好了
这是一道比较坑的题,坑如下:
1、最后那一个人必须在 tf - t前得到服务,否则无法得到服务
2、特判0个人的情况。
3、特判所有人都在服务结束后到来的情况。
4、如果所有人都已经服务完成,还有剩余时间。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define MOD 1000000007
#define pb push_back
#define mp make_pair
//#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef vector<int> vi;
typedef long long ll;
typedef unsigned long long ull;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};
const int N = 1e7+5;
ll n,s,f,t,x;
map<ll,ll> a;
map<ll,ll>::iterator it;
ll sum[100005];
int main()
{
cin >> s>>f>>t;
cin >> n;
rep(i,1,n)
{
cin >>x;
a[x]++;
}
ll re = inf_ll;
ll tim;
ll st;
// sum数组代表 在第k个人时,前k-1人的都得到服务后的时间
sum[0] = s;
ll ct=1;
for(it = a.begin();it!=a.end();it++)
{
st = t*it->se;
sum[ct] = max(sum[ct-1],it->first)+st;
ct++;
}
//2、特判0个人的情况。
//3、特判所有人都在服务结束后到来的情况。
if(n==0||a.begin()->first>=f)
{
cout <<s<<endl;
return 0;
}
ct = 0;
for(it = a.begin();it!=a.end();it++)
{
if(sum[ct]>f) break;
st = it->first;
// st-1>=0&&st-1+t<=f 最后那一个人必须在 tf - t前得到服务,否则无法得到服
if(sum[ct]-st+1 < re&&st-1>=0&&st-1+t<=f)
{
re = sum[ct]-st+1;
if(re<=0)
{
cout <<st-1<<endl;
return 0;
}
tim = st-1;
}
ct++;
}
//4、如果所有人都已经服务完成,还有剩余时间。
if(sum[a.size()]<=f-t)
tim = sum[a.size()];
cout <<tim<<endl;
return 0;
}