CF #398B The Queue

The Queue

本人渣翻:https://www.zybuluo.com/zhangche0526/note/666142
time limit: per test1 second
memory limit: per test256 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.


完全不需要什么算法,然而对细节处理的要求很高,考试时候以为很简单,不过好像题意理解地不清楚(果然英语水平有待提高),有一些细节的特殊情况处理地不好,没过,太可惜了

思路:

best为最短等待时间,ans为去的时刻,ctime储存当前最早能轮到自己的时刻

主要思路就是在每个人去的前一分钟去有可能是最短的,不过需要加一些特判:

  • n=0时直接输出开始时间
  • 用第一个人到达时间点前一分钟的情况作为best和ans的初始值
  • 枚举情况时,如果发现中间某一时刻队列为空,则ans就是这个时间,直接输出
  • 最后还要判断,如果最后一个人办理完后还有时间给自己办理,那就在最后一人办理完后再去

#include<iostream>
#include<cstdio>
typedef long long ll;
const int INF=1e9;
using namespace std;

ll ts, tf, t;
int n;
ll a[100000];
ll best = INF;
ll ans = 0;

int main() 
{
    cin>>ts>>tf>>t;
    scanf("%d", &n);
    for(int i=0;i<n;i++) scanf("%I64d",&a[i]);
    if(!n)
    {
        cout << ts << endl;
        return 0;
    }
    if(a[0]) 
    {
        best = ts - (a[0] - 1);
        ans = min(ts, a[0] - 1);
    }
    ll ctime = max(ts, a[0]) + t;
    for(int i = 1; i < n; ++i) 
    {
        if(ctime + t > tf)
            break;
        if(a[i] > a[i - 1]) 
        {
            if(a[i] - 1 >= ctime) 
            {
                cout << ctime << endl;
                return 0;
            }
            if(ctime - a[i] + 1 < best) 
            {
                best = ctime - a[i] + 1;
                ans = a[i] - 1;
            }
        }
        ctime=max(ctime, a[i])+t;
    }
    if(ctime+t<=tf)
    {
        best= 0;
        ans = ctime;
    }
    cout << ans << endl;
    return 0;
}
### 配置和运行小车仿真环境 要在 Ubuntu 20.04 上配置并运行小车仿真环境,可以按照以下方式操作: #### 安装 ROS Noetic 和 Gazebo 为了在 Ubuntu 20.04 中构建完整的仿真环境,首先需要安装 ROS Noetic 版本及其依赖项。可以通过以下命令完成安装过程[^1]: ```bash sudo sh -c &#39;echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list&#39; sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C1CF6E31E6BADE8868B172B4F42ED6FBAB17C654 sudo apt update sudo apt install ros-noetic-desktop-full ``` 接着初始化 `rosdep` 并更新其数据库: ```bash sudo rosdep init rosdep update ``` 如果遇到超时问题,则可尝试更换国内镜像源来解决问题[^2]。 #### 创建工作空间 建立一个新的 Catkin 工作区用于存储所有的项目文件: ```bash mkdir -p ~/catkin_ws/src cd ~/catkin_ws/ catkin_make source devel/setup.bash ``` #### 下载与编译仿真包 下载所需的仿真软件包至 src 文件夹下,并执行 catkin 编译流程: ```bash git clone https://github.com/your-repo-url.git ~/catkin_ws/src/hunter_simulation_package_name cd ~/catkin_ws && catkin_make ``` #### 设置默认参数简化启动流程 为了避免频繁重复输入相同指令,在 .bashrc 文件里加入必要的路径声明和其他预设选项能够极大地方便后续的操作步骤[^4]: 编辑 `.bashrc` 文件并将下面的内容追加进去: ```bash export TURTLEBOT3_MODEL=waffle_pi source /opt/ros/noetic/setup.bash source ~/catkin_ws/devel/setup.bash alias turtlebot3=&#39;roslaunch turtlebot3_gazebo turtlebot3_world.launch&#39; ``` 保存更改后重新加载该脚本使其生效: ```bash source ~/.bashrc ``` #### 启动仿真器及建图功能 现在可以直接通过定义好的别名快速开启整个模拟场景而无需额外指定任何参数: ```bash turtlebot3 ``` 之后依据具体需求调用相应的 launch 文件来进行 SLAM 建图或其他任务处理[^3]. ```python import rospy from geometry_msgs.msg import Twist def move(): velocity_publisher = rospy.Publisher(&#39;/cmd_vel&#39;, Twist, queue_size=10) vel_msg = Twist() # Setting the current time for distance calculus. t0 = float(rospy.Time.now().to_sec()) current_distance = 0 while(current_distance < 2): vel_msg.linear.x = 0.5 vel_msg.angular.z = 0 velocity_publisher.publish(vel_msg) t1=float(rospy.Time.now().to_sec()) current_distance= 0.5*(t1-t0) if __name__ == &#39;__main__&#39;: try: rospy.init_node(&#39;robot_cleaner&#39;, anonymous=True) move() except rospy.ROSInterruptException: pass ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值