国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Codeforces Round #398 B. The Queue

2019-11-08 02:44:55
字體:
來源:轉載
供稿:網友

Description

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 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.

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 2210 13

output

12

input

8 17 343 4 5 8

output

2

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.

題意

Vasya 準備排隊去辦理護照。已知只有一個窗口在提供服務,且提供服務的時間在 [ts,tf) ,同時每個人辦理業務都需要 t 時間。假設 Vasya 知道當天有 n 個人會去排隊辦理護照,且每個人到達的時間已知。問 Vasya 應該在何時去排隊,能使等待的時間最少。當窗口工作的時間到達 tf 時刻時,無論是否辦理完當前這個人的業務,均直接結束,不予繼續。若 Vasya 與其他人同時到達,則 Vasya 會排在這群人的最后。

分析

貌似這是我打 CF 以來經歷的最坑的 B 題。

首先進行預處理,已知每個人進入隊列的時間 s[i] ,當不考慮 Vasya 排隊的情況,每人結束服務的時間 e[i] 可通過前一個人的結束時間獲得。若前一個人的結束時間 e[i-1]+1 >= s[i] ,則當前這個人的結束時間記為 e[i] = e[i-1] + t 。否則可以認為在 e[i-1]+1 時刻隊列為空, Vasya 在此時來辦理業務無需等待。

在處理完 e[] 數組后,嘗試在每一個人開始時間前 1 時刻 s[i]-1 讓 Vasya 去排隊,計算他需要等待(此處加上了他辦理業務的用時)的時間為 e[i]-(s[i]-1)+1 = e[i]-s[i]+2 。取最小的等待時間對應的 s[i]-1 即可。

綜合上述想法,仍然有 n=0 的情況以及在處理完最后一個人的業務后仍有充裕時間供 Vasya 辦理業務的情況。特判。

代碼

#include<bits/stdc++.h>using namespace std;const long long inf = 1e13;long long s[100010];long long e[100010];long long ts, tf, t, n;int main(){ scanf("%I64d %I64d %I64d %d",&ts,&tf,&t,&n); for(int i=0;i<n;i++) scanf("%I64d",&s[i]); memset(e, -1, sizeof(e)); sort(s, s+n); long long now = ts; for(int i=0;i<n;i++) { if(s[i] <= now) { e[i] = now + t-1; now += t; } else { printf("%I64d/n",now); return 0; } if(now >= tf) break; } if(now + t - 1 <= tf) { printf("%I64d/n", now); return 0; } long long ans = 0, wat = inf; for(int i=0;i<n;i++) { if(e[i] == -1) break; if(i && s[i] == s[i-1]) continue; if(wat > e[i] - s[i] + 2) ans = s[i]-1, wat = e[i]-s[i] + 2; } if(e[n-1] != -1 && e[n-1]+t < tf) ans = e[n-1]+1; printf("%I64d/n",ans); return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 兰坪| 轮台县| 兰坪| 江安县| 洛南县| 兴业县| 临桂县| 红桥区| 章丘市| 山阳县| 南京市| 烟台市| 新密市| 荥阳市| 密山市| 龙岩市| 文化| 万年县| 土默特右旗| 阿克陶县| 保定市| 贵定县| 庆元县| 大同县| 团风县| 台江县| 尉犁县| 松阳县| 桐城市| 眉山市| 洪雅县| 兴山县| 五华县| 黎城县| 绥棱县| 思茅市| 阿合奇县| 海城市| 石台县| 榆中县| 兴海县|