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

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

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

题意:接待员的工作时间,结束时间,处理每个人的时间分别为ts,tf,t。现在有n个人明天将到达窗口的时间x[1],x[2]...x[n],,问明天什么时候去排队等待的时间最少。如果和某一个人同时到达,排在这个人的后面。数据保证答案可以买到票。

思路:模拟题。排在i-1~i人之间。比较x[i-1],x[i]-1与处理完i-1人的时候的时间sign进行比较。

代码:

 #include<bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAXN=1e5+,INF=0x3f3f3f3f,MOD=1e9+;
ll x[MAXN];
int main()
{
ll ts,tf,t;
scanf("%lld%lld%lld",&ts,&tf,&t);
int n;
scanf("%d",&n);
for(int i=; i<=n; i++) scanf("%lld",&x[i]);
x[]=,x[n+]=tf+;
ll sign=ts;
ll Min=tf,ans=-;
for(int i=; i<=n+; i++)
{
if(sign>tf-t) break;
if(x[i-]==x[i]) {sign+=t;continue;}
///x[i-1]~x[i]-1;
if(x[i-]>sign)
{
if(x[i-]-sign<Min)
Min=x[i-]-sign,ans=x[i-];
}
else if(x[i]-<sign)
{
if(sign-(x[i]-)<Min)
Min=sign-(x[i]-),ans=x[i]-;
}
else
{
cout<<sign<<endl;
return ;
}
sign+=t;
}
cout<<ans<<endl;
return ;
}

Codeforces 767B. The Queue 模拟题的更多相关文章

  1. CodeForces 767B The Queue

    模拟. 情况有点多,需要仔细.另外感觉题目的$tf$有点不太对......而且数据水了. $0$ $5$ $2$ $2$ $0$ $5$ 这组数据按照题意的话答案可以是$2$和$4$,但是好多错的答案 ...

  2. CodeForces - 344D Alternating Current (模拟题)

    id=46667" style="color:blue; text-decoration:none">CodeForces - 344D id=46667" ...

  3. Codeforces 691C. Exponential notation 模拟题

    C. Exponential notation time limit per test: 2 seconds memory limit per test:256 megabytes input: st ...

  4. CodeForces - 344B Simple Molecules (模拟题)

    CodeForces - 344B id=46665" style="color:blue; text-decoration:none">Simple Molecu ...

  5. CodeForces - 344E Read Time (模拟题 + 二分法)

    E. Read Time time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  6. CodeForces 681C Heap Operations (模拟题,优先队列)

    题意:给定 n 个按顺序的命令,但是可能有的命令不全,让你补全所有的命令,并且要求让总数最少. 析:没什么好说的,直接用优先队列模拟就行,insert,直接放入就行了,removeMin,就得判断一下 ...

  7. CodeForces - 427B (模拟题)

    Prison Transfer Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Sub ...

  8. Educational Codeforces Round 2 A. Extract Numbers 模拟题

    A. Extract Numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/600/pr ...

  9. Codeforces Beta Round #7 B. Memory Manager 模拟题

    B. Memory Manager 题目连接: http://www.codeforces.com/contest/7/problem/B Description There is little ti ...

随机推荐

  1. opencv 对RGB图像直接二值化

    #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...

  2. Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag.

    https://blog.csdn.net/watermusicyes/article/details/44963773 Context中有一个startActivity方法,Activity继承自C ...

  3. springboot 线程池

    我们常用ThreadPoolExecutor提供的线程池服务,springboot框架提供了@Async注解,帮助我们更方便的将业务逻辑提交到线程池中异步执行,今天我们就来实战体验这个线程池服务: 本 ...

  4. Python memoryview() 函数

    Python memoryview() 函数  Python 内置函数 描述 memoryview() 函数返回给定参数的内存查看对象(Momory view). 所谓内存查看对象,是指对支持缓冲区协 ...

  5. Selenium 定位元素原理,基本API,显示等待,隐式等待,重试机制等等

    Selenium  如何定位动态元素: 测试的时候会遇到元素每次变动的情况,例如: <div id="btn-attention_2030295">...</di ...

  6. Codeforces977D ---Divide by three, multiply by two 深搜+map存出现的数

    传送门:点我 题意:给定n长度的序列,重排成后一个数是前一个数除以三,或者后一个数是前一个数乘二,要求输出这个序列. 思路:大力深搜,对每个数搜除3的和乘2的是否出现过,然后继续搜下去.如果有一个数搜 ...

  7. httpclient和htpUrlConnection用例

    使用了很久框架,突然不知道,原生访问网络请求方式,回过头去看,还好,对现在流行使用okhttp也有了更深体会. HttpURLConnection介绍: HttpURLConnection是一种多用途 ...

  8. Git 分支 - 分支管理

    1 查看每一个分支 git branch 2 查看每一个分支的最后一次提交 git branch -v 3 创建分支 (1)只创建本地分支:git branch <branchname> ...

  9. gdb打印C++容器

    将以下内容保存成 .gdbinit 文件放到你的根目录,或者在gdb中source这个文件可以加载. 直接print容器即可. # # STL GDB evaluators/views/utiliti ...

  10. Delphi: 模态窗体最小化

    源起: 近期所介入的几个项目中,最后视频生成窗体,为一模态对话框.因生成时间可能较长,所以其窗体可以最小化,它最小化时同时最小化主程序,唤醒时主程序再复原. 代码亦是8年前本人所写,一直那样用了,也没 ...