【codeforces 767B】The Queue

【题目链接】:http://codeforces.com/contest/767/problem/B

【题意】

排队去办护照;
给你n个人何时来的信息;
然后问你应该何时去才能在队伍中等待的时间最短;
(如果你和别人同时到,你要等到和你同时到的人全都办完了才轮到你);

【题解】

细节题吧。
首先大体思路就是;
枚举那个人在哪个人办完之后办;
如果那个人办完之后和下一个时间的人之间有空隙->直接输出那个枚举的人办完后的时间->因为这表示等待时间为0的情况;
否则如果没有空隙,那么就要在下一个时间的人之前一个单位的时间来;
这样等待的时间尽量最短;
然后取最小值就好;
当然还有一种就是在第一个人之前半,或者没有一个人直接在ts时刻来就好;
这两类取较小值就好;
坑点:
题目没讲清楚的就是
如果在时间点20,服务一个人要10分钟,然后tf=30,那么如果这个时刻来人
那个人是可以被服务的。
初始时间要分t[1]< ts和t[1]>=ts两类赋值;

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define ps push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%lld",&x)
#define ref(x) scanf("%lf",&x)

typedef pair<int, int> pii;
typedef pair<LL, LL> pll;

const int dx[9] = { 0,1,-1,0,0,-1,-1,1,1 };
const int dy[9] = { 0,0,0,-1,1,-1,1,-1,1 };
const double pi = acos(-1.0);
const int N = 1e5+100;

LL ts,tf;
LL T[N],now,t,idxtt=-1,tt;
int n;

int main()
{
    //freopen("D:\\rush.txt", "r", stdin);
    rel(ts),rel(tf);rel(t);
    rei(n);
    rep1(i,1,n)
        rel(T[i]);
    if (T[1]<ts)
    now = ts;
    else
    now = T[1];
    rep1(i,1,n)
    {
        int l = i,r = i;
        while (r+1<=n && T[r+1]==T[l]) r++;
        int num = r-l+1;
        LL extcost=0;
        now+=1LL*t*num;
        if (now+t>tf) break;
        if (r+1<=n)
        {
            if (now<=T[r+1]-1)
            {
                printf("%lld\n",now);
                return 0;
            }
            else
            {
                if (now>=T[r+1])
                {
                    LL idx = T[r+1]-1,ttemp = 0;
                    ttemp += now-idx;
                    if (idxtt==-1)
                    {
                        idxtt = idx;
                        tt = ttemp;
                    }
                    else
                        if (tt>ttemp)
                        {
                            idxtt = idx;
                            tt = ttemp;
                        }
                }
            }
        }
        else
        {
                printf("%lld\n",now);
                return 0;
        }
        i = r;
    }
    if (n==0 || T[1]>ts)
    {
        return printf("%lld\n",ts),0;
    }
    else
        //t[1]<=ts
    {
        LL spt = T[1]-1;
        LL wait = ts-spt;
        if (idxtt==-1 || wait<tt)
            idxtt = spt;
    }
    printf("%lld\n",idxtt);
    //printf("\n%.2lf sec \n", (double)clock() / CLOCKS_PER_SEC);
    return 0;
}

转载于:https://www.cnblogs.com/AWCXV/p/7626502.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值