codeforces 767B The Queue【贪心】

链接:

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);
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值