传送门:点击打开链接
题意:Vasya要去护照,然而接待员在【ts,tf)的时间段内上班,处理一件业务需要花费时间t,并且有n个人在T【i】的时间去办护照,当Vasya与另一个人同时去办护照时,Vasya排在他的后面,问Vasya什么时候去等待的时间最少。
分析:肯定要在某个人前一秒到达,到的更早没有意义,到的更晚就被他抢先了。 但是要考虑各种情况,比如在所有人之后到、别人到的时间都不在规定时间内。
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define inf 0x3f3f3f3f
#define eps 1e-10
const int N = 1e5+5;
ll s,e,t,n,a[N];
int main() {
ios::sync_with_stdio(0);
cin>>s>>e>>t>>n;
for(int i=1;i<=n;i++) cin>>a[i];
if(a[1]>s) cout<<s<<endl;
else {
ll now=s,d=1e15,ans;///now记录当前时间 d记录最少等待时间 ans记录到达时间
for(int i=1;i<=n;i++) {
if(max(0LL,now-a[i]+1)<d && a[i]-1+t<=e) {
d=max(0LL,now-a[i]+1);
ans=a[i]-1;
}
now=max(now,a[i])+t;
}
if(now+t<=e) cout<<now<<endl;
else cout<<ans<<endl;
}
}
参考博客:https://blog.csdn.net/sdfzyhx/article/details/55670915