链接:
http://codeforces.com/contest/767/problem/B
题意:
Vasya要去护照,然而接待员在【ts,tf)的时间段内上班,处理一件业务需要花费时间t,并且有n个人在T【i】的时间去办护照,当Vasya与另一个人同时去办护照时,Vasya排在他的后面,问Vasya什么时候去等待的时间最少。
题解:
将n个人的办护照的时间段都计算出来,然后因为对于Vasya来说他只要在某个人来之前的前一秒先到所花费的时间最少,还要注意判断最后一个人办完护照后Vasya是否还能办护照。
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
#include <string.h>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<time.h>
using namespace std;
#define MAX_N 1000005
#define inf 0x3f3f3f3f
#define LL long long
#define ull unsigned long long
const LL INF = 9e18;
const int mod = 100000000;
typedef pair<LL, LL>P;
int n;
LL ts, tf, t;
LL T[MAX_N];
P V[MAX_N];
int main()
{
cin >> ts >> tf >> t;
cin >> n;
for(int i=1; i<=n; i++) {
scanf("%lld",&T[i]);
}
V[0].second = -INF;
for(int i=1; i<=n; i++) {
if(T[i] > V[i-1].second) {
if(T[i] >= ts)
V[i].first = T[i];
else
V[i].first = ts;
V[i].second = V[i].first + t - 1;
}
else {
V[i].first = V[i-1].second + 1;
V[i].second = V[i].first + t - 1;
}
}/*
for(int i=1; i<=n; i++) {
printf("%lld %lld\n",V[i].first, V[i].second);
}*/
LL minT = INF;
LL ans;
for(int i=1; i<=n; i++) {
P tmp;
if(T[i]-1 > V[i-1].second) {
if(T[i]-1 >= ts)
tmp.first = T[i] - 1;
else
tmp.first = ts;
tmp.second = tmp.first + t - 1;
}
else {
tmp.first = V[i-1].second + 1;
tmp.second = tmp.first + t - 1;
}
if(minT>tmp.first-T[i]+1 && tmp.second<tf) {
minT = tmp.first - T[i] + 1;
ans = T[i] - 1;
}
}
if(V[n].second+t<tf)
ans = V[n].second + 1;
if(minT != INF)
printf("%lld\n",ans);
else
printf("%lld\n",ts);
}