CF1776E.Crossing the Railways

NOI/NOI+/CTSC

通过率:0%

时间限制:4.00s

内存限制:1024MB

AC君温馨提醒

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

题目描述

Isona is in a train station. This station has two platforms, and between them there are mm parallel railways that can be viewed as infinite straight lines. Each railway is identified with an integer from 11 to mm, railway 11 being the closest to the first platform and railway mm being the farthest. There is a 11 meter distance between consecutive railways, as well as between each platform and its closest railway.

Isona is standing on the inner border of the first platform, when she realizes that she forgot to validate her ticket! There is a validating machine on the second platform, exactly opposite her current position (thus, the distance between Isona and the validating machine is m+1m + 1 meters). There are only ss seconds left to validate the ticket and the bridge designated to cross the railways is too far from the validating machine. Therefore, Isona (who is very brave and a little bit careless) will cross the railways running in a straight line perpendicular to the railways themselves. Isona can only run forward (not backward) and she can stay still. When she runs at maximum speed, she needs vv seconds to traverse 11 meter. She can run at any speed less than or equal to her maximum speed.

There is only one problem: nn trains are programmed to transit through the railways. The ii-th train will use the railway rir_i. It will start crossing the straight line between Isona and the validating machine aia_i seconds from now and it will end bib_i seconds from now. Of course, Isona cannot cross a railway when a train is passing. Formally, for every i=1, 2, …, ni = 1, \, 2, \, \dots, \, n, Isona is not allowed to be on railway rir_i at any time tt with ai<t<bia_i \lt t \lt b_i (but she is allowed to cross at times aia_i or bib_i).

The following picture summarizes the situation. In the picture there are m=4m = 4 railways and two trains are visible; the train going through railway 33 is currently crossing the line between Isona and the validating machine.

Isona is a really good runner, but she gets tired every time she has to change her running speed. What is the minimum number of speed changes she has to perform to get to the validating machine on the other platform within ss seconds from now? Note that at the beginning Isona is not running. She can start to run anytime. The instant she starts to run (i.e. her speed becomes positive) is not counted as a speed change.

伊索娜正在一座火车站内。该车站有两座站台,其间有 mm 条彼此平行的铁路线,可视为无限长的直线。每条铁路线用一个从 11 到 mm 的整数标识:铁路 11 距离第一座站台最近,铁路 mm 距离第二座站台最近。相邻两条铁路线之间的距离为 11 米;每座站台与其邻近的铁路线之间也相距 11 米。

伊索娜正站在第一座站台的内侧边缘,此时她突然意识到自己忘记验票了!第二座站台上恰好正对着她当前位置的位置设有一台验票机(因此,伊索娜与验票机之间的距离为 m+1m + 1 米)。距离验票截止仅剩 ss 秒,而专供横跨铁路线的人行天桥离验票机太远。因此,伊索娜(她非常勇敢,又略有些粗心)决定以垂直于所有铁路线的方向沿一条直线奔跑穿过这些铁路线。伊索娜只能向前跑(不能后退),也可以静止不动。当她以最大速度奔跑时,穿越 11 米需要 vv 秒;她可以以任意不超过其最大速度的速度奔跑。

但存在唯一一个问题:共有 nn 列火车按计划驶过这些铁路线。第 ii 列火车将驶经铁路线 rir_i,它将在当前时刻起 aia_i 秒后开始穿越伊索娜与验票机之间的连线,并于当前时刻起 bib_i 秒后结束穿越。显然,伊索娜在列车经过时不得穿越对应铁路线。形式化地说,对每个 i=1, 2, …, ni = 1,\,2,\,\dots,\,n,伊索娜在任意时刻 tt 满足 ai<t<bia_i < t < b_i 时均不可位于铁路线 rir_i 上(但在时刻 aia_i 或 bib_i 穿越是允许的)。

下图概括了该场景。图中显示 m=4m = 4 条铁路线,且可见两列火车;其中一列正行驶在铁路线 33 上,此刻正穿越伊索娜与验票机之间的连线。

伊索娜是一位出色的跑者,但每次改变奔跑速度都会令她感到疲惫。那么,在当前起 ss 秒内成功抵达对岸站台上的验票机,她最少需要改变多少次速度?注意:初始时刻伊索娜处于静止状态,她可以在任意时刻开始奔跑;她开始奔跑的瞬间(即速度由零变为正值)不计为一次速度变化。

输入格式

The first line of the input contains four integers nn, mm, ss, vv (1≤n≤5001 \leq n \leq 500, 1≤m≤101 \leq m \leq 10, 1≤s,v≤1091 \leq s, v \leq 10^9) — the number of trains, the number of railways, the maximum time in seconds Isona can spend crossing the railways, and the number of seconds she needs to traverse 11 meter at maximum speed.

Each of the next nn lines contains three integers aia_i, bib_i, rir_i (1≤ai<bi≤1091 \leq a_i \lt b_i \leq 10^9, 1≤ri≤m1 \leq r_i \leq m) — the start and end times of the ii-th train crossing the straight line between Isona and the validating machine, and the railway it will be using.

It is guaranteed that, for any two trains ii and jj that go through the same railway (i.e. ri=rjr_i = r_j), there is at least 11 second between them (that is, either aj≥bi+1a_j \ge b_i + 1 or ai≥bj+1a_i \ge b_j + 1).

输入的第一行包含四个整数 nn、mm、ss、vv(1≤n≤5001 \leq n \leq 500,1≤m≤101 \leq m \leq 10,1≤s,v≤1091 \leq s, v \leq 10^9)——分别表示列车数量、铁路数量、Isona 穿越铁路所能花费的最大时间(单位:秒),以及她以最大速度行走 11 米所需的时间(单位:秒)。

接下来的 nn 行中,每行包含三个整数 aia_i、bib_i、rir_i(1≤ai<bi≤1091 \leq a_i \lt b_i \leq 10^9,1≤ri≤m1 \leq r_i \leq m)——分别表示第 ii 列车穿越 Isona 与验证机之间直线段的起始时刻和结束时刻,以及该列车所使用的铁路编号。

保证:对任意两条经过同一条铁路的列车 ii 和 jj(即 ri=rjr_i = r_j),它们的时间区间至少相隔 11 秒(即满足 aj≥bi+1a_j \ge b_i + 1 或 ai≥bj+1a_i \ge b_j + 1)。

输出格式

Print the minimum number of speed changes Isona has to perform to get to the validating machine in time. If this is impossible, print −1-1.

输出 Isona 为及时到达验证机所需的最小速度变化次数。若无法实现,则输出 −1-1。

输入输出样例

  • 输入#1

    4 3 5 1
    1 2 1
    3 4 1
    2 3 2
    3 4 3

    输出#1

    0
  • 输入#2

    3 3 12 2
    2 10 1
    1 6 2
    8 12 3

    输出#2

    2
  • 输入#3

    8 4 13 2
    1 4 1
    5 13 1
    1 5 2
    6 13 2
    1 9 3
    10 13 3
    1 10 4
    11 13 4

    输出#3

    2
  • 输入#4

    1 1 2 2
    1 2 1

    输出#4

    -1

说明/提示

In the first sample, if Isona starts running at time t=0t=0 at maximum speed (11 m/s), she will cross each railway just when a train is about to traverse it, and she will arrive at the other platform at time 4=s−14 = s - 1 without changing speed.

In the second sample, a possible solution with 22 speed changes is the following: for the first 22 seconds Isona goes at maximum speed (0.50.5 m/s), then she slows down to 0.250.25 m/s for 44 seconds until she reaches the second railway. At that point, she goes at maximum speed again until she reaches the other platform.

In the third sample, Isona can wait 22 seconds before starting running. She then runs for 55 seconds at maximum speed (0.50.5 m/s). After that, she waits for 11 second not running (or running at 00 m/s), and finally she runs again at maximum speed for the last 55 seconds. Overall, she changes speed twice.

在第一个样例中,如果伊索娜在 t=0t=0 时刻以最大速度(11 m/s)起跑,她将在每列火车即将通过各铁路道口的时刻恰好穿越它们,并以恒定速度抵达对面站台,到达时间为 4=s−14 = s - 1。

在第二个样例中,一种需进行 22 次变速的可行方案如下:前 22 秒伊索娜以最大速度(0.50.5 m/s)奔跑;随后将速度降至 0.250.25 m/s 并持续 44 秒,直至抵达第二处铁路道口;此时她再次以最大速度奔跑,直至抵达对面站台。

在第三个样例中,伊索娜可先静止等待 22 秒再起跑;接着以最大速度(0.50.5 m/s)奔跑 55 秒;之后静止等待 11 秒(或以 00 m/s 的速度“奔跑”);最后再以最大速度奔跑最后 55 秒。整个过程中,她共改变速度两次。

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

首页