B. The Queue codeforces398div2

B. The Queue
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Finally! Vasya have come of age and that means he can finally get a passport! To do it, he needs to visit the passport office, but it's not that simple. There's only one receptionist at the passport office and people can queue up long before it actually opens. Vasya wants to visit the passport office tomorrow.

He knows that the receptionist starts working after ts minutes have passed after midnight and closes after tf minutes have passed after midnight (so that (tf - 1) is the last minute when the receptionist is still working). The receptionist spends exactly tminutes on each person in the queue. If the receptionist would stop working within t minutes, he stops serving visitors (other than the one he already serves).

Vasya also knows that exactly n visitors would come tomorrow. For each visitor Vasya knows the point of time when he would come to the passport office. Each visitor queues up and doesn't leave until he was served. If the receptionist is free when a visitor comes (in particular, if the previous visitor was just served and the queue is empty), the receptionist begins to serve the newcomer immediately.

"Reception 1"

For each visitor, the point of time when he would come to the passport office is positive. Vasya can come to the office at the time zero (that is, at midnight) if he needs so, but he can come to the office only at integer points of time. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and stand in the queue after the last of them.

Vasya wants to come at such point of time that he will be served by the receptionist, and he would spend the minimum possible time in the queue. Help him!

Input

The first line contains three integers: the point of time when the receptionist begins to work ts, the point of time when the receptionist stops working tf and the time the receptionist spends on each visitor t. The second line contains one integer n — the amount of visitors (0 ≤ n ≤ 100 000). The third line contains positive integers in non-decreasing order — the points of time when the visitors arrive to the passport office.

All times are set in minutes and do not exceed 1012; it is guaranteed that ts < tf. It is also guaranteed that Vasya can arrive at the passport office at such a point of time that he would be served by the receptionist.

Output

Print single non-negative integer — the point of time when Vasya should arrive at the passport office. If Vasya arrives at the passport office at the same time with several other visitors, he yields to them and queues up the last. If there are many answers, you can print any of them.

Examples
input
10 15 2
2
10 13
output
12
input
8 17 3
4
3 4 5 8
output
2

题意:给定一个处理时间段,和每个人的处理时间。 给定排队的n个人的到达时间。求出要使等待时间最少的插队时间点。

思路:

感觉此题略坑,第一是题意没读懂,接待员在t2的时间点如果没处理完,就停止处理了,这样子不符合要求。

第二是感觉坑点比较多,特殊情况比较多。

思路简单,遍历n个数据求出每个人的等待时间,如果某个人出现等待时间小于0了(并且此人能够在t2时刻处理完!!!),就直接在他前面插队。否则就继续遍历找最小的等待时间点。

特殊情况比较多:没有人等待的时候、第一个人来的时间点就超过了t2、只有一个人等待的时候(看处理时间和处理时间段的关系)、第一个人在t1和t2之间到达(此情况 可以直接输出)、处理时间段只能处理一个人的情况。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<climits>
using namespace std;
long long t1, t2, k;//表示开始时刻,结束时刻,每个人的时间花费
long long n;
long long a[1000005];
long long sum = 0, res = 0, m = LONG_LONG_MAX;
int main()
{
    scanf("%lld %lld %lld %lld", &t1, &t2, &k, &n);
    for(int i = 0; i < n; ++i)
    {
        scanf("%lld", &a[i]);
    }
    if(n == 0)//如果没有人等待,t1时刻入队即可
    {
        cout << t1;
        return 0;
    }
    if(a[0]>=t2)//第一个人来的时间已经超过了t2
    {
        cout << t1;
        return 0;
    }
    if(n == 1)//如果只有一个人等待,如果k比总处理时间大,那么就在他前面插队a[0]-1,否则t2-1-k入队一定不需要等待
    {
        cout << (k<(t2-t1)?(t2-1-k):a[0]-1);
        return 0;
    }
    if(a[0] > t1)//如果第一个人在t1之后到,则只需在a[0]-1时刻插队,无需等待
    {
        cout << a[0]-1;
        return 0;
    }
    if(k >= t2 - t1)//处理时间小于等于k
    {
        cout << a[0]-1;
        return 0;
    }
    sum = t1-a[0];//此时第一个人在t1前就到达了,用sum表示第一个人的等待时间。

    for(int i = 1; i < n; ++i)//用sum表示每个人的等待时间
    {
        if(a[i]>=t2) break;
        sum = a[i-1] + sum + k - a[i];
        if(sum < 0)
        {
            cout << a[i]-1;
            return 0;
        }
        if(sum < m && a[i]+sum+k<=t2)
        {
            m = sum;
            res = a[i]-1;
        }
        if(i == n-1 && sum+2*k+a[i] <= t2)
        {
           cout << a[i]+sum+k;
           return 0;
        }
    }
    if(t1-a[0]+1 < m)//计算结果与从队首插队比较
    {
        m = t1-a[0]+1;
        res = a[0]-1;
    }
    cout << res;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值