B. The Queue

Codeforces
IDCF767B
Time1000ms
Memory256MB
Difficulty
brute forcegreedy
English · Original
Chinese · Translation
Formal · Original
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 _t__s_ minutes have passed after midnight and closes after _t__f_ minutes have passed after midnight (so that (_t__f_ - 1) is the last minute when the receptionist is still working). The receptionist spends exactly _t_ minutes 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. <center>![image](https://espresso.codeforces.com/9b4a83d8f6d5409fc88077d7ef97dcbfe63822f2.png) "Reception 1"</center>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 _t__s_, the point of time when the receptionist stops working _t__f_ 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 _t__s_ < _t__f_. 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. [samples] ## Note In the first example the first visitor comes exactly at the point of time when the receptionist begins to work, and he is served for two minutes. At 12 minutes after the midnight the receptionist stops serving the first visitor, and if Vasya arrives at this moment, he will be served immediately, because the next visitor would only come at 13 minutes after midnight. In the second example, Vasya has to come before anyone else to be served.
终于,瓦西里成年了,这意味着他可以申请护照了!为此,他需要前往护照办公室,但事情没那么简单。护照办公室只有一位接待员,人们常常在办公室开门前就排起长队。瓦西里计划明天去护照办公室。 他知道接待员在午夜后 #cf_span[ts] 分钟开始工作,在午夜后 #cf_span[tf] 分钟结束工作(因此 #cf_span[(tf - 1)] 是接待员仍在工作的最后一分钟)。接待员为每位排队者服务恰好 #cf_span[t] 分钟。如果在剩余时间不足 #cf_span[t] 分钟时仍有排队者,接待员将停止服务(但正在服务的那位除外)。 瓦西里还知道,明天将有恰好 #cf_span[n] 位访客到来。对于每位访客,瓦西里知道他们到达护照办公室的时间。每位访客排队等候,直到被服务。如果访客到达时接待员空闲(特别是,如果前一位访客刚被服务完毕且队列为空),接待员会立即开始服务这位新访客。 每位访客到达的时间均为正数。瓦西里可以选择在时间零(即午夜)到达办公室,但他只能在整数时刻到达。如果瓦西里与多位访客同时到达,他会礼让给他们,排在他们之后。 瓦西里希望选择一个到达时间,使得他能被接待员服务,且在队列中等待的时间最少。请帮助他! 第一行包含三个整数:接待员开始工作的时间 #cf_span[ts]、结束工作的时间 #cf_span[tf] 和每位访客的服务时间 #cf_span[t]。第二行包含一个整数 #cf_span[n] —— 访客数量(#cf_span[0 ≤ n ≤ 100 000])。第三行包含按非递减顺序排列的正整数,表示每位访客到达护照办公室的时间。 所有时间单位为分钟,且不超过 #cf_span[1012];保证 #cf_span[ts < tf]。同时保证瓦西里可以找到一个到达时间,使他能被接待员服务。 请输出一个非负整数 —— 瓦西里应到达护照办公室的时间。如果瓦西里与多位访客同时到达,他会排在他们之后。若有多个答案,输出任意一个即可。 在第一个例子中,第一位访客恰好在接待员开始工作时到达,被服务两分钟。在午夜后 12 分钟,接待员结束对第一位访客的服务,此时如果瓦西里到达,他将被立即服务,因为下一位访客要到午夜后 13 分钟才来。 在第二个例子中,瓦西里必须比任何人都早到才能被服务。 ## Input 第一行包含三个整数:接待员开始工作的时间 #cf_span[ts]、结束工作的时间 #cf_span[tf] 和每位访客的服务时间 #cf_span[t]。第二行包含一个整数 #cf_span[n] —— 访客数量(#cf_span[0 ≤ n ≤ 100 000])。第三行包含按非递减顺序排列的正整数,表示每位访客到达护照办公室的时间。所有时间单位为分钟,且不超过 #cf_span[1012];保证 #cf_span[ts < tf]。同时保证瓦西里可以找到一个到达时间,使他能被接待员服务。 ## Output 请输出一个非负整数 —— 瓦西里应到达护照办公室的时间。如果瓦西里与多位访客同时到达,他会排在他们之后。若有多个答案,输出任意一个即可。 [samples] ## Note 在第一个例子中,第一位访客恰好在接待员开始工作时到达,被服务两分钟。在午夜后 12 分钟,接待员结束对第一位访客的服务,此时如果瓦西里到达,他将被立即服务,因为下一位访客要到午夜后 13 分钟才来。在第二个例子中,瓦西里必须比任何人都早到才能被服务。
Let $ t_s $, $ t_f $, and $ t $ be given integers: the start time, end time, and service duration, respectively. Let $ n $ be the number of pre-scheduled visitors. Let $ a_1 \leq a_2 \leq \dots \leq a_n $ be the arrival times of the visitors (positive integers, in non-decreasing order). Define the service completion time for each visitor recursively: - Let $ s_0 = t_s $ (the time the receptionist becomes available). - For each visitor $ i = 1, 2, \dots, n $: $$ s_i = \max(s_{i-1}, a_i) + t $$ (the receptionist starts serving visitor $ i $ at $ \max(s_{i-1}, a_i) $, and finishes at $ s_i $). Vasya can arrive at any integer time $ x \geq 0 $. He will be served if and only if there exists a time $ x $ such that: - $ x \leq t_f - t $ (so that service can start by $ t_f - t $ at latest), - and the receptionist is free at time $ x $, i.e., $ x \geq s_{i} $ for the last visitor $ i $ who finished before $ x $, and $ x + t \leq t_f $. Vasya arrives **after** all other visitors arriving at the same time. We seek the **earliest possible** arrival time $ x \in \mathbb{Z}_{\geq 0} $ such that: - $ x + t \leq t_f $, - and $ x \geq \max(s_{i}, a_{i+1}) $ for some $ i $, meaning he arrives **immediately after** the $ i $-th visitor finishes **and before** the $ (i+1) $-th visitor arrives (or there is no next visitor). The possible candidate times for Vasya’s arrival are: 1. $ x = t_s $ — if $ t_s \leq t_f - t $, and no visitor arrives before $ t_s $, or the last visitor before $ t_s $ finishes by $ t_s $. 2. Between visitors: for each $ i = 0, 1, \dots, n $, consider the interval $ [s_i, a_{i+1}) $, where $ a_0 = -\infty $, $ a_{n+1} = \infty $, and $ s_0 = t_s $. The earliest available slot in this interval is $ \max(s_i, t_s) $, but since $ s_i \geq t_s $, we take $ x = s_i $, provided $ s_i + t \leq t_f $ and $ s_i < a_{i+1} $ (if $ i < n $). 3. After all visitors: $ x = s_n $, if $ s_n + t \leq t_f $. We must choose the **smallest** such $ x \geq 0 $ satisfying: - $ x \in \mathbb{Z} $, - $ x \geq t_s $ (since receptionist starts at $ t_s $), - $ x + t \leq t_f $, - $ x \geq s_i $ for some $ i \in \{0, 1, \dots, n\} $, - and $ x < a_{i+1} $ if $ i < n $, or no constraint if $ i = n $. Thus, the candidate set is: $$ \mathcal{C} = \left\{ s_i \;\middle|\; 0 \leq i \leq n,\; s_i + t \leq t_f,\; \text{and}\; (i = n \text{ or } s_i < a_{i+1}) \right\} $$ where $ s_0 = t_s $, and $ s_i = \max(s_{i-1}, a_i) + t $ for $ i \geq 1 $. Then, the answer is: $$ \boxed{\min \mathcal{C}} $$ (Note: Since $ s_i $ are computed in increasing order, and $ s_i \geq t_s $, the first valid $ s_i $ in the sequence that satisfies $ s_i + t \leq t_f $ and $ s_i < a_{i+1} $ (or $ i = n $) is the optimal answer.)
Samples
Input #1
10 15 2
2
10 13
Output #1
12
Input #2
8 17 3
4
3 4 5 8
Output #2
2
API Response (JSON)
{
  "problem": {
    "name": "B. The Queue",
    "description": {
      "content": "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 o",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 1000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF767B"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "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 o...",
      "is_translate": false,
      "language": "English"
    },
    {
      "statement_type": "Markdown",
      "content": "终于,瓦西里成年了,这意味着他可以申请护照了!为此,他需要前往护照办公室,但事情没那么简单。护照办公室只有一位接待员,人们常常在办公室开门前就排起长队。瓦西里计划明天去护照办公室。\n\n他知道接待员在午夜后 #cf_span[ts] 分钟开始工作,在午夜后 #cf_span[tf] 分钟结束工作(因此 #cf_span[(tf - 1)] 是接待员仍在工作的最后一分钟)。接待员为每位排队者服务恰好 ...",
      "is_translate": true,
      "language": "Chinese"
    },
    {
      "statement_type": "Markdown",
      "content": "Let $ t_s $, $ t_f $, and $ t $ be given integers: the start time, end time, and service duration, respectively.  \nLet $ n $ be the number of pre-scheduled visitors.  \nLet $ a_1 \\leq a_2 \\leq \\dots \\l...",
      "is_translate": false,
      "language": "Formal"
    }
  ]
}
Full JSON Raw Segments