CodeForces 767 B.The Queue(贪心)

Description

n n 个人去办业务,工作人员在午夜过后ts分钟开始上班,在午夜过后 tf t f 分钟下班,即第 tf1 t f − 1 分钟是其最后一分钟上班时间,给一个人办业务需要 t t 分钟,这n个人分别在午夜过后 ai a i 分钟开始等待,只有队列前面的人办完业务且工作人员剩余工作时间不小于 t t 时当前人才能办业务,现在小明想办业务,问如果要其可以办到业务且等待时间最少,他需要什么时候去等待,注意小明排在和其同时到达的人最后

Input

第一行三个整数ts,tf,t分别表示上班时间、下班时间和服务一个人的用时,之后输入一整数 n n 表示人数,最后输入n个不减的数 a1,...,an a 1 , . . . , a n 表示这 n n 个人来此等待的时刻,保证小明来的足够早就可以办到业务

(0n105,0ts<tf1012,1t,ai1012)

Output

输出小明去等待的时刻,使得他一定可以办到业务且等待时间最短

Sample Input

10 15 2
2
10 13

Sample Output

12

Solution

易知小明要么在某个人之前一分钟去,要么在前面若干人办完业务后的时刻去,以此更新最优解即可

Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define maxn 111111
ll ts,tf,t,a[maxn];
int n;
int main()
{
    while(~scanf("%I64d%I64d%I64d",&ts,&tf,&t))
    {
        scanf("%d",&n);
        for(int i=1;i<=n;i++)scanf("%I64d",&a[i]);
        if(n==0)
        {
            printf("%I64d\n",ts);
            continue;
        }
        ll ans=ts,anst=0;
        if(a[1]-1<ts)ans=ts-(a[1]-1),anst=a[1]-1;
        else ans=0,anst=ts;
        ll sum=ts;
        for(int i=1;i<=n;i++)
        {
            sum=max(sum,a[i]);
            sum+=t;
            if(i==n)
            {
                if(sum+t<=tf)ans=0,anst=sum;
            }
            else
            {
                if(sum<a[i+1]&&sum+t<=tf)ans=0,anst=sum;
                else if(a[i]<a[i+1]&&max(sum,a[i+1]-1)+t<=tf)
                {
                    if(ans>sum-(a[i+1]-1))
                        ans=sum-(a[i+1]-1),anst=a[i+1]-1;
                }
            }
        }
        printf("%I64d\n",anst);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值