CF1340C.Nastya and Unexpected Guest

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

If the girl doesn't go to Denis, then Denis will go to the girl. Using this rule, the young man left home, bought flowers and went to Nastya.

On the way from Denis's house to the girl's house is a road of nn lines. This road can't be always crossed in one green light. Foreseeing this, the good mayor decided to place safety islands in some parts of the road. Each safety island is located after a line, as well as at the beginning and at the end of the road. Pedestrians can relax on them, gain strength and wait for a green light.

Denis came to the edge of the road exactly at the moment when the green light turned on. The boy knows that the traffic light first lights up gg seconds green, and then rr seconds red, then again gg seconds green and so on.

Formally, the road can be represented as a segment [0,n][0, n]. Initially, Denis is at point 00. His task is to get to point nn in the shortest possible time.

He knows many different integers d1,d2,…,dmd_1, d_2, \ldots, d_m, where 0≤di≤n0 \leq d_i \leq n — are the coordinates of points, in which the safety islands are located. Only at one of these points, the boy can be at a time when the red light is on.

Unfortunately, Denis isn't always able to control himself because of the excitement, so some restrictions are imposed:

  • He must always move while the green light is on because it's difficult to stand when so beautiful girl is waiting for you. Denis can change his position by ±1\pm 1 in 11 second. While doing so, he must always stay inside the segment [0,n][0, n].
  • He can change his direction only on the safety islands (because it is safe). This means that if in the previous second the boy changed his position by +1+1 and he walked on a safety island, then he can change his position by ±1\pm 1. Otherwise, he can change his position only by +1+1. Similarly, if in the previous second he changed his position by −1-1, on a safety island he can change position by ±1\pm 1, and at any other point by −1-1.
  • At the moment when the red light is on, the boy must be on one of the safety islands. He can continue moving in any direction when the green light is on.

Denis has crossed the road as soon as his coordinate becomes equal to nn.

This task was not so simple, because it's possible that it is impossible to cross the road. Since Denis has all thoughts about his love, he couldn't solve this problem and asked us to help him. Find the minimal possible time for which he can cross the road according to these rules, or find that it is impossible to do.

如果女孩不去找丹尼斯,那么丹尼斯就会去找女孩。依据这条规则,这位年轻人离开家,买了鲜花,动身前往娜斯佳那里。

从丹尼斯家到女孩家的路上,有一条由 nn 段组成的道路。这条道路无法总是在一次绿灯期间全部通过。预见到这一点,善良的市长决定在道路的某些位置设置安全岛。每个安全岛位于某一段道路之后,此外道路的起点(00 处)和终点(nn 处)也设有安全岛。行人可以在安全岛上休息、恢复体力,并等待下一次绿灯。

丹尼斯恰好在绿灯亮起的那一刻抵达道路的起点。他知道交通灯先持续亮 gg 秒绿灯,再持续亮 rr 秒红灯,然后再次亮 gg 秒绿灯,如此循环往复。

形式化地,该道路可表示为线段 [0,n][0, n]。初始时,丹尼斯位于点 00。他的目标是以最短时间抵达点 nn。

他知晓一组互不相同的整数 d1,d2,…,dmd_1, d_2, \ldots, d_m,其中 0≤di≤n0 \leq d_i \leq n,这些是安全岛所在位置的坐标。仅在这些点上,当红灯亮起时,男孩才被允许停留。

不幸的是,由于激动情绪的影响,丹尼斯并不能总是很好地控制自己,因此存在如下限制:

  • 他必须始终在绿灯亮起时移动——毕竟,如此美丽的女孩正在等待,站着不动可太难了!丹尼斯每秒可以将自身位置改变 ±1\pm 1(即向左或向右走一步),且移动过程中必须始终保持在区间 [0,n][0, n] 内。
  • 他只能在安全岛上改变行进方向(因为只有那里是安全的)。这意味着:若前一秒他向右移动(位置变化为 +1+1)且当时所处位置是一个安全岛,则下一秒他可选择向左或向右移动(即位置变化为 ±1\pm 1);否则,他只能继续向右移动(位置变化为 +1+1)。类似地,若前一秒他向左移动(位置变化为 −1-1),且当时所处位置是一个安全岛,则下一秒他可选择向左或向右移动;而在其他任意非安全岛位置,他只能继续向左移动(位置变化为 −1-1)。
  • 当红灯亮起时,男孩必须位于某个安全岛上。绿灯亮起后,他可朝任意方向继续移动。

当丹尼斯的坐标首次等于 nn 时,即视为成功穿越道路。

本题并不简单,因为可能存在根本无法穿越道路的情况。由于丹尼斯满脑子都是对爱情的憧憬,他无法独立解决这个问题,于是请求我们协助。请找出他依据上述规则穿越道路所需的最短可能时间;若不可能完成,则判定为无解。

输入格式

The first line contains two integers nn and mm (1≤n≤106,2≤m≤min(n+1,104))(1 \leq n \leq 10^6, 2 \leq m \leq min(n + 1, 10^4)) — road width and the number of safety islands.

The second line contains mm distinct integers d1,d2,…,dmd_1, d_2, \ldots, d_m (0≤di≤n)(0 \leq d_i \leq n) — the points where the safety islands are located. It is guaranteed that there are 00 and nn among them.

The third line contains two integers g,rg, r (1≤g,r≤1000)(1 \leq g, r \leq 1000) — the time that the green light stays on and the time that the red light stays on.

第一行包含两个整数 nn 和 mm (1≤n≤106,2≤m≤min(n+1,104))(1 \leq n \leq 10^6, 2 \leq m \leq min(n + 1, 10^4)) —— 道路宽度和安全岛的数量。

第二行包含 mm 个互不相同的整数 d1,d2,…,dmd_1, d_2, \ldots, d_m (0≤di≤n)(0 \leq d_i \leq n) —— 安全岛所在的位置。保证其中包含 00 和 nn。

第三行包含两个整数 g,rg, r (1≤g,r≤1000)(1 \leq g, r \leq 1000) —— 绿灯持续时间和红灯持续时间。

输出格式

Output a single integer — the minimum time for which Denis can cross the road with obeying all the rules.

If it is impossible to cross the road output −1-1.

输出一个整数——Denis 遵守所有规则穿过马路所需的最短时间。

如果无法穿过马路,则输出 −1-1。

输入输出样例

  • 输入#1

    15 5
    0 3 7 14 15
    11 11

    输出#1

    45
  • 输入#2

    13 4
    0 3 7 13
    9 9

    输出#2

    -1

说明/提示

In the first test, the optimal route is:

  • for the first green light, go to 77 and return to 33. In this case, we will change the direction of movement at the point 77, which is allowed, since there is a safety island at this point. In the end, we will be at the point of 33, where there is also a safety island. The next 1111 seconds we have to wait for the red light.
  • for the second green light reaches 1414. Wait for the red light again.
  • for 11 second go to 1515. As a result, Denis is at the end of the road.

In total, 4545 seconds are obtained.

In the second test, it is impossible to cross the road according to all the rules.

在第一次测试中,最优路径为:

  • 对于第一个绿灯,前往 77 并返回至 33。此时,我们将在点 77 处改变行进方向,这是允许的,因为该点设有安全岛。最终,我们将到达点 33,此处也设有安全岛。接下来的 1111 秒需等待红灯。
  • 对于第二个绿灯,抵达 1414。再次等待红灯。
  • 用 11 秒时间前往 1515。最终,丹尼斯到达道路终点。

总计耗时 4545 秒。

在第二次测试中,无法依据所有规则穿越道路。

输入解题思路,AI测评打分。不知道怎么写?

首页